name: Pack Release files
on:
  release:
    types: [published]

jobs:
  DispatchZGMBuild:
    name: Dispatch MissionZGM build job
    runs-on: windows
    steps:
      - name: Diagnostics
        run: |
          echo "repository: ${{ gitea.repository }}"
          echo "ref:  ${{ gitea.ref }}"
          echo "ref_name: ${{ gitea.ref_name }}"
          echo "sha: ${{ gitea.sha }}"
          echo "api_url: ${{ gitea.api_url }}"
          echo "workflow: ${{ gitea.workflow }}"

      - name: Publish remote issue
        run: |
          $headers = @{
            "accept" = "application/json"
            "Authorization" = "token ${{ secrets.RUNNER_ISSUE_TOKEN }}"
          }
          $response = Invoke-RestMethod -Uri "${{ gitea.api_url }}/repos/17th-Ranger-Battalion-ORG/MissionsForZGM/issues" -Method Post -Headers $headers -ContentType "application/json" -Body "{`"title`": `"publish-release`"}"

  CreateRelease:
    name: Publish cleaned template
    runs-on: windows
    steps:
      - name: Diagnostics
        run: |
          echo "repository: ${{ gitea.repository }}"
          echo "ref:  ${{ gitea.ref }}"
          echo "ref_name: ${{ gitea.ref_name }}"
          echo "sha: ${{ gitea.sha }}"
          echo "api_url: ${{ gitea.api_url }}"
          echo "workflow: ${{ gitea.workflow }}"

      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: 'main'
          path: release

      - name: Prepare template
        run: |
          mkdir MissionTemplate
          cd MissionTemplate
          Get-ChildItem ..\release\ | Copy-Item -Recurse
          Remove-Item -Recurse aaFrameworkUtils
          Remove-Item -Recurse framework\x
          Remove-Item -Recurse .git*
          Get-ChildItem
      
      - name: Create archive
        run: |
          cd release
          $revision = git rev-parse --short=10 HEAD
          $tag = git describe --abbrev=0 --tags
          cd ..
          Compress-Archive MissionTemplate mission-maker-template-v$tag-$revision.zip

      - name: Add archive to release
        uses: https://gitea.com/actions/gitea-release-action@v1.3.0
        with:
          files: |-
            mission-maker-template*.zip
