name: "Release Plugin"

on:
  workflow_dispatch:

permissions:
  contents: write
  packages: write

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Read version from VERSION.txt
        id: read-version
        run: |
          MAIN_VERSION=$(cat VERSION.txt)
          echo "MAIN_VERSION=${MAIN_VERSION}"
          echo "MAIN_VERSION=$MAIN_VERSION" >> $GITHUB_ENV

      - name: Check for BETA file
        id: check-beta
        run: |
          if [ -f "BETA" ]; then
            echo "IS_BETA=true" >> $GITHUB_ENV
          else
            echo "IS_BETA=false" >> $GITHUB_ENV
          fi

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.0.x

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"

      - name: Minify HTML
        run: |
          npx html-minifier-terser --collapse-boolean-attributes --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true -o IntroSkipper/Configuration/configPage.html IntroSkipper/Configuration/configPage.html
          npx terser IntroSkipper/Configuration/inject.js -o IntroSkipper/Configuration/inject.js -c -m
          npx terser IntroSkipper/Configuration/visualizer.js -o IntroSkipper/Configuration/visualizer.js -c -m

      - name: Restore Beta dependencies
        if: ${{env.IS_BETA == 'true' }}
        run: |
          dotnet nuget add source --username ${{ github.repository_owner }} --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name jellyfin-pre "https://nuget.pkg.github.com/jellyfin/index.json"
          dotnet tool install --global dotnet-outdated-tool
          dotnet outdated -pre Always -u -inc Jellyfin

      - name: Restore dependencies
        if: ${{env.IS_BETA == 'false' }}
        run: dotnet restore

      - name: Run update version
        uses: intro-skipper/intro-skipper-action-ts@main
        with:
          task-type: "updateVersion"

      - name: Embed version info
        run: |
          GITHUB_SHA=${{ github.sha }}
          sed -i "s/string\.Empty/\"$GITHUB_SHA\"/g" IntroSkipper/Helper/Commit.cs

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Create archive
        run: zip -j "intro-skipper-v${{ env.NEW_FILE_VERSION }}.zip" IntroSkipper/bin/Release/net8.0/IntroSkipper.dll

      - name: Remove old release if exits
        if: ${{ github.repository == 'intro-skipper/intro-skipper-test' }}
        run: gh release delete "${{ env.MAIN_VERSION }}/v${{ env.NEW_FILE_VERSION }}" --cleanup-tag --yes || true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create new release with tag
        if: github.event_name != 'pull_request'
        run: gh release create "${{ env.MAIN_VERSION }}/v${{ env.NEW_FILE_VERSION }}" "intro-skipper-v${{ env.NEW_FILE_VERSION }}.zip" --title "v${{ env.NEW_FILE_VERSION }}" --latest --generate-notes --target ${{ env.MAIN_VERSION }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Run validation and update script
        uses: intro-skipper/intro-skipper-action-ts@main
        with:
          task-type: "updateManifest"
        env:
          GITHUB_REPO_VISIBILITY: ${{ github.event.repository.visibility }}
          MAIN_VERSION: ${{ env.MAIN_VERSION }}

      - name: Commit changes
        if: success()
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git add README.md manifest.json IntroSkipper/IntroSkipper.csproj .github/ISSUE_TEMPLATE/bug_report_form.yml
          git commit -m "release v${{ env.NEW_FILE_VERSION }}"
          git push