name: Nightly Release

on:
  schedule:
    # Run every day at 01:00 UTC
    - cron: "0 1 * * *"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  check_commits:
    runs-on: ubuntu-latest
    outputs:
      has_commits: ${{ steps.check_commits.outputs.has_commits }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: "main"

      - name: Check for commits from yesterday
        id: check_commits
        run: |
          # Get yesterday's date in YYYY-MM-DD format
          YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
          echo "Checking for commits from: $YESTERDAY"

          # Check if there were any commits yesterday
          COMMIT_COUNT=$(git log --oneline --since="$YESTERDAY 00:00:00" --until="$YESTERDAY 23:59:59" | wc -l)

          echo "Found $COMMIT_COUNT commits from yesterday"

          if [ $COMMIT_COUNT -gt 0 ]; then
            echo "has_commits=true" >> $GITHUB_OUTPUT
            echo "✅ Found $COMMIT_COUNT commits from yesterday - continuing workflow"
          else
            echo "has_commits=false" >> $GITHUB_OUTPUT
            echo "❌ No commits found from yesterday - skipping remaining steps"
            echo "::notice::❌ No commits found from yesterday - skipping remaining steps"
          fi

  build:
    if: ${{ needs.check_commits.outputs.has_commits == 'true' }}
    needs: [check_commits]
    uses: ./.github/workflows/reusable-build.yml
    with:
      is_release: false
      retention_days: 40

  create-macos-fat-binary:
    if: ${{ needs.check_commits.outputs.has_commits == 'true' }}
    needs: [build]
    outputs:
      resolc-universal-apple-darwin_url: ${{ steps.set-output.outputs.resolc-universal-apple-darwin_url }}
      resolc-universal-apple-darwin_sha: ${{ steps.set-output.outputs.resolc-universal-apple-darwin_sha }}
    runs-on: macos-15
    steps:
      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Create macOS Fat Binary
        run: |
          lipo resolc-aarch64-apple-darwin resolc-x86_64-apple-darwin -create -output resolc-universal-apple-darwin

      - name: Make Executable
        run: |
          chmod +x resolc-universal-apple-darwin

      - uses: actions/upload-artifact@v4
        id: artifact-upload-step
        with:
          name: resolc-universal-apple-darwin
          path: resolc-universal-apple-darwin
          retention-days: 40

      - name: Set output variables
        id: set-output
        env:
          TARGET: resolc-universal-apple-darwin
        run: |
          echo "${TARGET}_url=${{ steps.artifact-upload-step.outputs.artifact-url }}" >> "$GITHUB_OUTPUT"
          echo "${TARGET}_sha=${{ steps.artifact-upload-step.outputs.artifact-digest }}" >> "$GITHUB_OUTPUT"

  generate-nightly-json:
    runs-on: ubuntu-24.04
    if: ${{ needs.check_commits.outputs.has_commits == 'true' }}
    environment: tags
    needs: [build, create-macos-fat-binary, check_commits]
    steps:
      - name: Checkout revive
        uses: actions/checkout@v4
        with:
          path: revive

      - name: Checkout resolc-bin
        uses: actions/checkout@v4
        with:
          repository: paritytech/resolc-bin
          path: resolc-bin

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true
          path: bins

      - uses: actions/create-github-app-token@v1
        id: app-token
        with:
          app-id: ${{ secrets.REVIVE_JSON_APP_ID }}
          private-key: ${{ secrets.REVIVE_JSON_APP_KEY }}
          owner: paritytech
          repositories: resolc-bin

      - name: Generate JSON
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TOKEN: ${{ steps.app-token.outputs.token }}
          APP_NAME: "paritytech-revive-json"
          Green: "\e[32m"
          NC: "\e[0m"
        run: |
          echo '[' > data.json
          echo '${{ toJSON(needs.build.outputs) }}' >> data.json
          echo ',' >> data.json
          echo '${{ toJSON(needs.create-macos-fat-binary.outputs) }}' >> data.json
          echo ']' >> data.json
          chmod +x bins/resolc-x86_64-unknown-linux-musl
          export FIRST_SOLC_VERSION=$(./bins/resolc-x86_64-unknown-linux-musl --supported-solc-versions | cut -f 1 -d "," | tr -d ">=")
          export LAST_SOLC_VERSION=$(./bins/resolc-x86_64-unknown-linux-musl --supported-solc-versions | cut -f 2 -d "," | tr -d "<=")
          export FILEPATH=$(readlink -f data.json)
          export TAG=$(cd revive;gh release list --json name,isLatest --jq '.[] | select(.isLatest)|.name')
          cd resolc-bin
          mkdir -p nightly
          cd nightly
          python3 ../../revive/.github/scripts/json_generator_nightly.py
          cd ..
          git status

          echo "${Green}Add new remote with gh app token${NC}"
          git remote set-url origin $(git config remote.origin.url | sed "s/github.com/${APP_NAME}:${TOKEN}@github.com/g")

          echo "${Green}Remove http section that causes issues with gh app auth token${NC}"
          sed -i.bak '/\[http/d' ./.git/config
          sed -i.bak '/extraheader/d' ./.git/config

          git config user.email "ci@parity.io"
          git config user.name "${APP_NAME}"

          git add nightly/
          git commit -m "Update nightly json"
          git push origin main

          echo "::notice::nightly info.list files were successfully published to https://github.com/paritytech/resolc-bin"
