name: Build and release to TestFlight and GitHub
on:
  workflow_dispatch:
    inputs:
      build_ios:
        description: 'Build iOS (TestFlight)'
        type: boolean
        default: true
      build_tvos:
        description: 'Build tvOS (TestFlight)'
        type: boolean
        default: false
      build_mac_beta:
        description: 'Build macOS (TestFlight)'
        type: boolean
        default: false
      build_mac_notarized:
        description: 'Build macOS (notarized)'
        type: boolean
        default: false
      create_release:
        description: 'Create GitHub release'
        type: boolean
        default: true

concurrency:
  group: release
  cancel-in-progress: false

env:
  APP_NAME: Yattee
  FASTLANE_USER: ${{ secrets.FASTLANE_USER }}
  FASTLANE_PASSWORD: ${{ secrets.FASTLANE_PASSWORD }}
  ITC_TEAM_ID: ${{ secrets.ITC_TEAM_ID }}
  TEAM_ID: ${{ secrets.TEAM_ID }}
  DEVELOPER_KEY_ID: ${{ secrets.DEVELOPER_KEY_ID }}
  DEVELOPER_KEY_ISSUER_ID: ${{ secrets.DEVELOPER_KEY_ISSUER_ID }}
  DEVELOPER_KEY_CONTENT: ${{ secrets.DEVELOPER_KEY_CONTENT }}
  TEMP_KEYCHAIN_USER: ${{ secrets.TEMP_KEYCHAIN_USER }}
  TEMP_KEYCHAIN_PASSWORD: ${{ secrets.TEMP_KEYCHAIN_PASSWORD }}
  DEVELOPER_APP_IDENTIFIER: ${{ secrets.DEVELOPER_APP_IDENTIFIER }}
  GIT_AUTHORIZATION: ${{ secrets.GIT_AUTHORIZATION }}
  MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
  CERTIFICATES_GIT_URL: ${{ secrets.CERTIFICATES_GIT_URL }}
  TESTFLIGHT_EXTERNAL_GROUPS: ${{ secrets.TESTFLIGHT_EXTERNAL_GROUPS }}

jobs:
  determine_build_number:
    name: Determine build number
    runs-on: macos-26
    outputs:
      build_number: ${{ steps.calc.outputs.build_number }}
      version_number: ${{ steps.version.outputs.version_number }}
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4'
          bundler-cache: true
          cache-version: 1
      - uses: maierj/fastlane-action@v3.0.0
        with:
          lane: latest_build_number
      - name: Calculate next build number
        id: calc
        run: |
          LATEST=$(cat latest_build_number.txt)
          NEXT=$((LATEST + 1))
          echo "build_number=$NEXT" >> $GITHUB_OUTPUT
      - name: Read version number
        id: version
        run: |
          VERSION=$(grep -m 1 MARKETING_VERSION Yattee.xcodeproj/project.pbxproj | cut -d' ' -f3 | sed 's/;//g')
          echo "version_number=$VERSION" >> $GITHUB_OUTPUT

  ios_beta:
    if: ${{ inputs.build_ios }}
    needs: [determine_build_number]
    name: Release iOS to TestFlight
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4'
          bundler-cache: true
          cache-version: 1
      - name: Set build number
        run: |
          sed -i '' 's/CURRENT_PROJECT_VERSION = [0-9]*/CURRENT_PROJECT_VERSION = ${{ needs.determine_build_number.outputs.build_number }}/' Yattee.xcodeproj/project.pbxproj
      - name: Clear SPM cache
        run: rm -rf ~/Library/Caches/org.swift.swiftpm/artifacts
      - uses: maierj/fastlane-action@v3.0.0
        with:
          lane: ios beta
      - uses: actions/upload-artifact@v4
        with:
          name: ios-beta-build
          path: fastlane/builds/**/*.ipa
          if-no-files-found: ignore

  tvos_beta:
    if: ${{ inputs.build_tvos }}
    needs: [determine_build_number]
    name: Release tvOS to TestFlight
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4'
          bundler-cache: true
          cache-version: 1
      - name: Set build number
        run: |
          sed -i '' 's/CURRENT_PROJECT_VERSION = [0-9]*/CURRENT_PROJECT_VERSION = ${{ needs.determine_build_number.outputs.build_number }}/' Yattee.xcodeproj/project.pbxproj
      - name: Clear SPM cache
        run: rm -rf ~/Library/Caches/org.swift.swiftpm/artifacts
      - uses: maierj/fastlane-action@v3.0.0
        with:
          lane: tvos beta
      - uses: actions/upload-artifact@v4
        with:
          name: tvos-beta-build
          path: fastlane/builds/**/*.ipa
          if-no-files-found: ignore

  mac_beta:
    if: ${{ inputs.build_mac_beta }}
    needs: [determine_build_number]
    name: Release macOS to TestFlight
    runs-on: macos-26
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4'
          bundler-cache: true
          cache-version: 1
      - name: Set build number
        run: |
          sed -i '' 's/CURRENT_PROJECT_VERSION = [0-9]*/CURRENT_PROJECT_VERSION = ${{ needs.determine_build_number.outputs.build_number }}/' Yattee.xcodeproj/project.pbxproj
      - name: Clear SPM cache
        run: rm -rf ~/Library/Caches/org.swift.swiftpm/artifacts
      - uses: maierj/fastlane-action@v3.0.0
        with:
          lane: mac beta
      - uses: actions/upload-artifact@v4
        with:
          name: mac-beta-build
          path: fastlane/builds/**/*.pkg
          if-no-files-found: ignore

  mac_notarized:
    if: ${{ inputs.build_mac_notarized }}
    needs: [determine_build_number]
    name: Build and notarize macOS app
    runs-on: macos-26
    env:
      BUILD_NUMBER: ${{ needs.determine_build_number.outputs.build_number }}
      VERSION_NUMBER: ${{ needs.determine_build_number.outputs.version_number }}
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.4'
          bundler-cache: true
          cache-version: 1
      - name: Set build number
        run: |
          sed -i '' 's/CURRENT_PROJECT_VERSION = [0-9]*/CURRENT_PROJECT_VERSION = ${{ env.BUILD_NUMBER }}/' Yattee.xcodeproj/project.pbxproj
      - name: Clear SPM cache
        run: rm -rf ~/Library/Caches/org.swift.swiftpm/artifacts
      - uses: maierj/fastlane-action@v3.0.0
        with:
          lane: mac build_and_notarize
      - run: |
          echo "APP_PATH=fastlane/builds/${{ env.VERSION_NUMBER }}-${{ env.BUILD_NUMBER }}/macOS/Yattee.app" >> $GITHUB_ENV
          echo "ZIP_PATH=fastlane/builds/${{ env.VERSION_NUMBER }}-${{ env.BUILD_NUMBER }}/macOS/Yattee-${{ env.VERSION_NUMBER }}-macOS.zip" >> $GITHUB_ENV
      - name: ZIP build
        run: /usr/bin/ditto -c -k --keepParent ${{ env.APP_PATH }} ${{ env.ZIP_PATH }}
      - uses: actions/upload-artifact@v4
        with:
          name: mac-notarized-build
          path: ${{ env.ZIP_PATH }}
          if-no-files-found: error

  release:
    if: ${{ inputs.create_release && !cancelled() && !failure() }}
    needs: [determine_build_number, ios_beta, tvos_beta, mac_beta, mac_notarized]
    name: Create GitHub release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    env:
      BUILD_NUMBER: ${{ needs.determine_build_number.outputs.build_number }}
      VERSION_NUMBER: ${{ needs.determine_build_number.outputs.version_number }}
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.REPO_TOKEN }}
      - name: Commit build number
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          sed -i 's/CURRENT_PROJECT_VERSION = [0-9]*/CURRENT_PROJECT_VERSION = ${{ env.BUILD_NUMBER }}/' Yattee.xcodeproj/project.pbxproj
          git add Yattee.xcodeproj/project.pbxproj
          git diff --cached --quiet && echo "Build number already up to date" || {
            git commit -m "Bump build number to ${{ env.BUILD_NUMBER }}"
            git push origin ${{ github.ref_name }}
          }
      - uses: actions/download-artifact@v4
        with:
          path: artifacts
      - uses: ncipollo/release-action@v1
        with:
          artifacts: artifacts/**/*.ipa,artifacts/**/*.zip,artifacts/**/*.pkg
          commit: ${{ github.ref_name }}
          tag: ${{ env.VERSION_NUMBER }}-${{ env.BUILD_NUMBER }}
          prerelease: true
          bodyFile: CHANGELOG.md

  update_altstore:
    needs: [release]
    uses: ./.github/workflows/update-altstore.yml
