name: Build and Release Plugin

on:
  push:
    branches:
      - main

jobs:
  build-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Extract plugin name and version
        id: plugin-info
        run: |
          PLUGIN_VERSION=$(grep "^version" plugin.info.txt | cut -d' ' -f2)
          BUILD_DATE=$(date +%Y%m%d.%H%M%S)
          TAG="v${PLUGIN_VERSION}-${BUILD_DATE}"
          echo "version=${PLUGIN_VERSION}" >> $GITHUB_OUTPUT
          echo "tag=${TAG}" >> $GITHUB_OUTPUT
          echo "Plugin Version: ${PLUGIN_VERSION}, Tag: ${TAG}"
      
      - name: Create plugin package
        run: |
          mkdir -p skillforge-package
          # Copy all plugin files except git-related and excluded files
          cp -r action.php admin.php helper.php syntax.php plugin.info.txt README.md LICENSE CHANGELOG.md style.css skillforge-package/
          cp -r classes/ conf/ lang/ skillforge-package/
          
          # Create ZIP archive with files at root level
          cd skillforge-package
          zip -r "../skillforge-${{ steps.plugin-info.outputs.version }}.zip" .
          cd ..
          ls -lh skillforge-*.zip
          echo "RELEASE_FILE=skillforge-${{ steps.plugin-info.outputs.version }}.zip" >> $GITHUB_ENV
      
      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.plugin-info.outputs.tag }}
          name: SkillForge ${{ steps.plugin-info.outputs.version }}
          body: |
            ## SkillForge DokuWiki Plugin
            
            **Version:** ${{ steps.plugin-info.outputs.version }}
            
            ### Installation
            
            1. Download the `skillforge-${{ steps.plugin-info.outputs.version }}.zip` file below
            2. Extract it in your DokuWiki plugins directory: `lib/plugins/`
            3. Go to Admin → SkillForge to configure
            
            ### What's included
            - Plugin core files (action.php, admin.php, helper.php, syntax.php)
            - Configuration templates (conf/)
            - Language files (lang/)
            - Documentation (README.md, CHANGELOG.md)
          files: |
            ${{ env.RELEASE_FILE }}
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
