name: Nightly Release

on:
  workflow_dispatch:
  schedule:
    - cron: "0 15 * * *"

permissions:
  contents: write

jobs:
  check-activity:
    name: Check repository activity
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.check_commits.outputs.should_run }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Check for recent commits
        id: check_commits
        run: |
          LAST_COMMIT_TIMESTAMP=$(git log -1 --format=%ct)
          NOW_TIMESTAMP=$(date +%s)
          # If the last commit is older than 24 hours (86400 seconds), skip the build
          if (( (NOW_TIMESTAMP - LAST_COMMIT_TIMESTAMP) > 86400 )); then
            echo "No new commits in the last 24 hours. Skipping build."
            echo "should_run=false" >> $GITHUB_OUTPUT
          else
            echo "New commits found. Proceeding with build."
            echo "should_run=true" >> $GITHUB_OUTPUT
          fi

  build-android:
    needs: check-activity
    if: needs.check-activity.outputs.should_run == 'true'
    uses: ./.github/workflows/android-build.yml
    secrets: inherit
    with:
      flutter_version: ${{ vars.FLUTTER_VERSION }}
      workflow_profile: nightly
      build_mode: release_common
      artifact_name: APK
      enable_build_cache: true
      enable_gradle_cache: true
      signing_required: true

  build-android-full:
    needs: check-activity
    if: needs.check-activity.outputs.should_run == 'true'
    uses: ./.github/workflows/android-build.yml
    secrets: inherit
    with:
      flutter_version: ${{ vars.FLUTTER_VERSION }}
      workflow_profile: nightly
      build_mode: release_full
      artifact_name: APK-FULL
      enable_build_cache: true
      enable_gradle_cache: true
      signing_required: true

  build-windows:
    needs: check-activity
    if: needs.check-activity.outputs.should_run == 'true'
    uses: ./.github/workflows/windows-build.yml
    with:
      flutter_version: ${{ vars.FLUTTER_VERSION }}
      workflow_profile: nightly
      artifact_name: Windows
      enable_build_cache: true
      package_release_artifacts: true
      msix_skip_clean: true

  build-linux:
    needs: check-activity
    if: needs.check-activity.outputs.should_run == 'true'
    uses: ./.github/workflows/linux-build.yml
    with:
      flutter_version: ${{ vars.FLUTTER_VERSION }}
      workflow_profile: nightly
      artifact_name: Linux
      package_release_artifacts: true

  release:
    name: Release
    runs-on: ubuntu-latest
    needs:
      [
        check-activity,
        build-android,
        build-android-full,
        build-windows,
        build-linux,
      ]
    if: needs.check-activity.outputs.should_run == 'true'
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download artifacts
        uses: actions/download-artifact@v4

      - name: Generate release body
        id: release_body
        run: |
          cat <<EOF >> $GITHUB_OUTPUT
          body<<DELIMITER
          Latest commit: $(git log -1 --pretty=%s)
          Commit hash: $(git rev-parse --short HEAD)
          Build date: $(date -u +'%Y-%m-%dT%H:%M:%SZ')
          DELIMITER
          EOF

      - name: Delete existing nightly release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release delete nightly --cleanup-tag -y || true

      - uses: ncipollo/release-action@v1
        with:
          artifacts: "APK/ciyue-*-release.apk,APK-FULL/ciyue-*-full.apk,Windows/*,Linux/*"
          tag: "nightly"
          name: "Nightly Build"
          body: ${{ steps.release_body.outputs.body }}
          prerelease: true
          allowUpdates: true
          replacesArtifacts: true
