name: Build Mod Package

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    runs-on: windows-latest
    environment: TgBot
    env:
      TG_CHAT_ID: -1003975565934
      TG_THREAD_ID: 2

    defaults:
      run:
        shell: pwsh

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Prepare workspace
        run: |
          $ErrorActionPreference = "Stop"

          if (-not (Test-Path -LiteralPath "Mods\\DnD 5.5e AIO Russian\\Localization\\Russian\\russian.xml")) {
              throw "Repository sources are not available in the runner workspace."
          }

          New-Item -ItemType Directory -Path ".tools\\lslib" -Force | Out-Null
          New-Item -ItemType Directory -Path "build" -Force | Out-Null

      - name: Resolve version tag
        id: vars
        run: |
          $ErrorActionPreference = "Stop"
          $versionTag = ""
          if ($env:GITHUB_REF -like "refs/tags/v*") {
              $versionTag = $env:GITHUB_REF_NAME
          }
          "version_tag=$versionTag" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8

      - name: Notify Telegram about build start
        if: startsWith(github.ref, 'refs/tags/v')
        continue-on-error: true
        env:
          BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
        run: |
          $ErrorActionPreference = "Stop"
          $target = if ("${{ steps.vars.outputs.version_tag }}") { "${{ steps.vars.outputs.version_tag }}" } else { "${{ github.ref_name }}" }
          $runUrl = "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          $text = "⏳ <b>Старт сборки релиза</b>`n<b>Репозиторий:</b> <code>${{ github.repository }}</code>`n<b>Тег:</b> <code>$target</code>`n<a href=`"$runUrl`">Открыть лог сборки</a>"
          .\scripts\send-telegram-notification.ps1 -BotToken $env:BOT_TOKEN -ChatId $env:TG_CHAT_ID -ThreadId $env:TG_THREAD_ID -Text $text -DisableNotification

      - name: Download latest LSLib release
        run: |
          $ErrorActionPreference = "Stop"
          $release = Invoke-RestMethod -Uri "https://api.github.com/repos/Norbyte/lslib/releases/latest"
          $asset = $release.assets | Where-Object { $_.name -match '\.zip$' } | Select-Object -First 1

          if (-not $asset) {
              throw "Could not find a downloadable LSLib zip asset in the latest release."
          }

          Invoke-WebRequest -UseBasicParsing -Uri $asset.browser_download_url -OutFile ".tools/lslib/lslib.zip"
          Expand-Archive -LiteralPath ".tools/lslib/lslib.zip" -DestinationPath ".tools/lslib" -Force

      - name: Build .pak
        run: |
          $ErrorActionPreference = "Stop"
          $divine = Get-ChildItem -Path ".tools\\lslib" -Recurse -File |
            Where-Object { $_.Name -ieq "Divine.exe" } |
            Select-Object -First 1

          if (-not $divine) {
              throw "Divine.exe was not found in the downloaded LSLib release."
          }

          & ".\\scripts\\build.ps1" -DivinePath $divine.FullName -Workspace "${{ github.workspace }}" -VersionTag "${{ steps.vars.outputs.version_tag }}"

      - name: Show build result
        run: |
          $ErrorActionPreference = "Stop"
          $archiveBaseName = "DnD 5.5e AIO Russian"
          if ($env:GITHUB_REF -like "refs/tags/v*") {
              $archiveBaseName = "DnD 5.5e AIO Russian $env:GITHUB_REF_NAME"
          }
          $zipPath = [System.IO.Path]::GetFullPath((Join-Path "${{ github.workspace }}" "build/$archiveBaseName.zip"))

          Get-ChildItem "build/DnD 5.5e AIO Russian.pak", "build/info.json", $zipPath |
            Select-Object FullName, Length, LastWriteTime

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: bg3-dnd55e-russian-localization-${{ github.run_number }}
          path: build/*
          if-no-files-found: error

      - name: Create or update GitHub release
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          $ErrorActionPreference = "Stop"
          $tagName = "${{ github.ref_name }}"
          $zipPath = Join-Path $env:GITHUB_WORKSPACE "build\\DnD 5.5e AIO Russian $tagName.zip"

          if (-not (Test-Path -LiteralPath $zipPath)) {
              throw "Release archive was not found at '$zipPath'."
          }

          gh release view $tagName --repo "${{ github.repository }}" *> $null

          if ($LASTEXITCODE -ne 0) {
              gh release create $tagName $zipPath `
                --repo "${{ github.repository }}" `
                --title $tagName `
                --notes ""
          }
          else {
              gh release upload $tagName $zipPath `
                --repo "${{ github.repository }}" `
                --clobber
          }

      - name: Notify Telegram about build success
        if: success() && startsWith(github.ref, 'refs/tags/v')
        continue-on-error: true
        env:
          BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
        run: |
          $ErrorActionPreference = "Stop"
          $releaseUrl = "${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }}"
          $text = "🏁 <b>Релиз собран успешно</b>`n<b>Тег:</b> <code>${{ github.ref_name }}</code>`n<a href=`"$releaseUrl`">Открыть готовый релиз</a>"
          .\scripts\send-telegram-notification.ps1 -BotToken $env:BOT_TOKEN -ChatId $env:TG_CHAT_ID -ThreadId $env:TG_THREAD_ID -Text $text -DisableNotification

      - name: Notify Telegram about build failure
        if: failure() && startsWith(github.ref, 'refs/tags/v')
        continue-on-error: true
        env:
          BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
        run: |
          $ErrorActionPreference = "Stop"
          $runUrl = "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          $text = "❌ <b>Сборка релиза завершилась ошибкой</b>`n<b>Тег:</b> <code>${{ github.ref_name }}</code>`n<a href=`"$runUrl`">Открыть лог сборки</a>"
          .\scripts\send-telegram-notification.ps1 -BotToken $env:BOT_TOKEN -ChatId $env:TG_CHAT_ID -ThreadId $env:TG_THREAD_ID -Text $text
