name: 'Release Plugin'

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version v'
        required: true
        type: string

permissions:
  contents: write

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'

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

    - name: Restore dependencies
      run: dotnet restore

    - name: Run update version
      run: node update-version.js
      env:
        VERSION: ${{ github.event.inputs.version }}

    - name: Embed version info
      run: echo "${{ github.sha }}" > ConfusedPolarBear.Plugin.IntroSkipper/Configuration/version.txt

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

    - name: Upload artifact
      uses: actions/upload-artifact@v4.3.3
      with:
        name: ConfusedPolarBear.Plugin.IntroSkipper-v${{ github.event.inputs.version }}.dll
        path: ConfusedPolarBear.Plugin.IntroSkipper/bin/Debug/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll
        if-no-files-found: error

    - name: Create archive
      uses: vimtor/action-zip@v1.2
      with:
        files: |
          ConfusedPolarBear.Plugin.IntroSkipper/bin/Debug/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll
        dest: intro-skipper-v${{ github.event.inputs.version }}.zip

    - name: Generate manifest keys
      run: |
        sourceUrl="https://github.com/${{ github.repository }}/releases/download/10.9/v${{ github.event.inputs.version }}/intro-skipper-v${{ github.event.inputs.version }}.zip"
        echo "SOURCE_URL=$sourceUrl" >> $GITHUB_ENV
        md5sum intro-skipper-v${{ github.event.inputs.version }}.zip > intro-skipper-v${{ github.event.inputs.version }}.md5
        checksum="$(awk '{print $1}' intro-skipper-v${{ github.event.inputs.version }}.md5)"
        echo "CHECKSUM=$checksum" >> $GITHUB_ENV
        timestamp="$(date +%FT%TZ)"
        echo "TIMESTAMP=$timestamp" >> $GITHUB_ENV

    - name: Publish release
      uses: 8bitDream/action-github-releases@v1.0.0
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        automatic_release_tag: 10.9/v${{ github.event.inputs.version }}
        prerelease: false
        title: v${{ github.event.inputs.version }}
        files: |
          intro-skipper-v${{ github.event.inputs.version }}.zip

    - name: Publish release notes
      uses: softprops/action-gh-release@v2.0.5
      with:
        tag_name: 10.9/v${{ github.event.inputs.version }}
        name: v${{ github.event.inputs.version }}
        draft: false
        prerelease: false
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Run validation and update script
      run: node validate-and-update-manifest.js
      env:
        VERSION: ${{ github.event.inputs.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 manifest.json ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj
        git commit -m "release v${{ github.event.inputs.version }}"
        git push