name: release-clients

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      version:
        description: "릴리즈 버전. 예: v0.1.0-alpha.1"
        required: true
        type: string
      channel:
        description: "릴리즈 채널"
        required: true
        default: alpha
        type: choice
        options:
          - alpha
          - beta
          - rc
          - stable
      upload_to_vps:
        description: "준비된 릴리즈 번들을 VPS 다운로드 호스트로 업로드"
        required: true
        default: false
        type: boolean

permissions:
  contents: read

jobs:
  build-windows:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          global-json-file: global.json

      - name: Install NSIS
        run: sudo apt-get update && sudo apt-get install -y nsis

      - name: Build Windows release assets
        env:
          VERSION_INPUT: ${{ github.event_name == 'workflow_dispatch' && inputs.version || github.ref_name }}
        run: |
          chmod +x scripts/release/build-windows-distributions.sh
          ./scripts/release/build-windows-distributions.sh \
            --version "$VERSION_INPUT" \
            --output out

          cp out/KoTalk-windows-x64-"$VERSION_INPUT".zip out/KoTalk-windows-x64.zip
          cp out/KoTalk-windows-x64-onefile-"$VERSION_INPUT".exe out/KoTalk-windows-x64-onefile.exe
          cp out/KoTalk-windows-x64-installer-"$VERSION_INPUT".exe out/KoTalk-windows-x64-installer.exe

      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows-release
          path: |
            out/KoTalk-windows-x64.zip
            out/KoTalk-windows-x64-onefile.exe
            out/KoTalk-windows-x64-installer.exe

  build-android:
    if: ${{ hashFiles('src/PhysOn.Mobile.Android/*.csproj') != '' }}
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: "17"

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          global-json-file: global.json

      - name: Install Android workload
        run: dotnet workload install android

      - name: Publish Android APK
        env:
          VERSION_INPUT: ${{ github.event_name == 'workflow_dispatch' && inputs.version || github.ref_name }}
        run: |
          chmod +x scripts/release/build-android-apk.sh
          ./scripts/release/build-android-apk.sh \
            --version "$VERSION_INPUT" \
            --output out

          cp out/KoTalk-android-universal-"$VERSION_INPUT".apk out/KoTalk-android-universal.apk

      - name: Upload Android artifact
        uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: out/KoTalk-android-universal.apk

  assemble-release:
    if: ${{ always() && needs.build-windows.result == 'success' && (needs.build-android.result == 'success' || needs.build-android.result == 'skipped') }}
    needs:
      - build-windows
      - build-android
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download Windows artifact
        uses: actions/download-artifact@v4
        with:
          name: windows-release
          path: incoming/windows

      - name: Download Android artifact
        continue-on-error: true
        uses: actions/download-artifact@v4
        with:
          name: android-apk
          path: incoming/android

      - name: Prepare release bundle
        env:
          VERSION_INPUT: ${{ github.event_name == 'workflow_dispatch' && inputs.version || github.ref_name }}
          CHANNEL_INPUT: ${{ github.event_name == 'workflow_dispatch' && inputs.channel || '' }}
        run: |
          chmod +x scripts/release/release-prepare-assets.sh

          channel="${CHANNEL_INPUT}"
          if [[ -z "$channel" ]]; then
            case "$VERSION_INPUT" in
              *alpha*) channel="alpha" ;;
              *beta*) channel="beta" ;;
              *rc*) channel="rc" ;;
              *) channel="stable" ;;
            esac
          fi

          prepare_args=(
            --version "$VERSION_INPUT"
            --channel "$channel"
            --windows-zip incoming/windows/KoTalk-windows-x64.zip
            --windows-portable-exe incoming/windows/KoTalk-windows-x64-onefile.exe
            --windows-installer-exe incoming/windows/KoTalk-windows-x64-installer.exe
            --force
          )

          if [[ -f incoming/android/KoTalk-android-universal.apk ]]; then
            prepare_args+=(--android-apk incoming/android/KoTalk-android-universal.apk)
          fi

          ./scripts/release/release-prepare-assets.sh \
            "${prepare_args[@]}"

      - name: Upload release bundle
        uses: actions/upload-artifact@v4
        with:
          name: release-bundle
          path: release-assets

  publish-forge-release:
    if: ${{ secrets.FORGE_RELEASE_TOKEN != '' }}
    needs: assemble-release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download release bundle
        uses: actions/download-artifact@v4
        with:
          name: release-bundle
          path: .

      - name: Publish release to forge
        env:
          VERSION_INPUT: ${{ github.event_name == 'workflow_dispatch' && inputs.version || github.ref_name }}
          FORGE_RELEASE_TOKEN: ${{ secrets.FORGE_RELEASE_TOKEN }}
        run: |
          chmod +x scripts/release/release-publish-forge.sh
          ./scripts/release/release-publish-forge.sh --version "$VERSION_INPUT"

  upload-to-vps:
    if: github.event_name == 'workflow_dispatch' && inputs.upload_to_vps
    needs: assemble-release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download release bundle
        uses: actions/download-artifact@v4
        with:
          name: release-bundle
          path: .

      - name: Configure SSH key
        env:
          DOWNLOAD_SSH_KEY: ${{ secrets.DOWNLOAD_SSH_KEY }}
        run: |
          test -n "$DOWNLOAD_SSH_KEY"
          install -d -m 700 ~/.ssh
          printf '%s\n' "$DOWNLOAD_SSH_KEY" > ~/.ssh/download_host
          chmod 600 ~/.ssh/download_host

      - name: Upload bundle to download host
        env:
          VERSION_INPUT: ${{ inputs.version }}
          DOWNLOAD_SSH_HOST: ${{ secrets.DOWNLOAD_SSH_HOST }}
          DOWNLOAD_SSH_USER: ${{ secrets.DOWNLOAD_SSH_USER }}
          DOWNLOAD_ROOT: ${{ secrets.DOWNLOAD_ROOT }}
        run: |
          test -n "$DOWNLOAD_SSH_HOST"
          test -n "$DOWNLOAD_SSH_USER"
          test -n "$DOWNLOAD_ROOT"
          chmod +x scripts/release/release-upload-assets.sh
          ./scripts/release/release-upload-assets.sh \
            --version "$VERSION_INPUT" \
            --host "$DOWNLOAD_SSH_HOST" \
            --user "$DOWNLOAD_SSH_USER" \
            --target "$DOWNLOAD_ROOT" \
            --ssh-key ~/.ssh/download_host
