---
name: Weekly Release
run-name: weekly-release

on:
  schedule:
    # Every Wednesday at 9:00 AM UTC
    - cron: "0 9 * * 3"
  workflow_dispatch:
    inputs:
      force:
        description: "Force release even without code changes"
        type: boolean
        default: false
      include_ci_changes:
        description: "Include .github/ changes in release check"
        type: boolean
        default: false

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: false

permissions:
  contents: write
  pull-requests: read

jobs:
  check-changes:
    name: Check for Changes
    runs-on: ubuntu-latest
    outputs:
      has_changes: ${{ steps.check.outputs.has_changes }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check for changes since last release
        id: check
        run: |
          # Force release if requested
          if [ "${{ inputs.force }}" == "true" ]; then
            echo "Force release requested"
            echo "has_changes=true" >> $GITHUB_OUTPUT
            exit 0
          fi

          # Get last release tag
          LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")

          if [ -z "$LAST_TAG" ]; then
            echo "No previous release found, proceeding with release"
            echo "has_changes=true" >> $GITHUB_OUTPUT
            exit 0
          fi

          echo "Last release tag: $LAST_TAG"

          # Get files changed since last release (excluding version bump commits)
          CHANGED_FILES=$(git log ${LAST_TAG}..HEAD --grep="chore(release):" --invert-grep --name-only --pretty=format: | sort -u | grep -v '^$' || echo "")

          if [ -z "$CHANGED_FILES" ]; then
            echo "No files changed since last release"
            echo "has_changes=false" >> $GITHUB_OUTPUT
            exit 0
          fi

          echo "Files changed since $LAST_TAG:"
          echo "$CHANGED_FILES"

          # Filter out .github/ changes unless override is set
          if [ "${{ inputs.include_ci_changes }}" != "true" ]; then
            RELEASE_FILES=$(echo "$CHANGED_FILES" | grep -v '^\.github/' || echo "")
          else
            RELEASE_FILES="$CHANGED_FILES"
            echo "(including .github/ changes due to manual override)"
          fi

          if [ -n "$RELEASE_FILES" ]; then
            echo "has_changes=true" >> $GITHUB_OUTPUT
            echo "Release needed - found changes outside .github/"
          else
            echo "has_changes=false" >> $GITHUB_OUTPUT
            echo "No release needed - only .github/ changes detected"
          fi

  release:
    name: Release
    needs: check-changes
    if: needs.check-changes.outputs.has_changes == 'true'
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.release.outputs.version }}
      tag: ${{ steps.release.outputs.tag }}
      skipped: ${{ steps.release.outputs.skipped }}
    steps:
      - name: Get token
        id: get_token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.ROVO_CD_APP_ID }}
          private-key: ${{ secrets.ROVO_CD_TOKEN }}
          owner: ${{ github.repository_owner }}
          repositories: ${{ github.event.repository.name }}

      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ steps.get_token.outputs.token }}

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Release with release-pilot
        id: release
        uses: a-line-services/release-pilot@v1
        with:
          github-token: ${{ steps.get_token.outputs.token }}
          cargo-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  publish-vscode:
    name: Publish VSCode Extension
    needs: release
    if: needs.release.outputs.skipped != 'true'
    uses: ./.github/workflows/vscode-publish.yml
    with:
      ref: ${{ needs.release.outputs.tag }}
    secrets: inherit

  publish-jetbrains:
    name: Publish JetBrains Plugin
    needs: release
    if: needs.release.outputs.skipped != 'true'
    uses: ./.github/workflows/jetbrains-publish.yml
    with:
      ref: ${{ needs.release.outputs.tag }}
    secrets: inherit

  no-release:
    name: No Release Needed
    needs: check-changes
    if: needs.check-changes.outputs.has_changes == 'false'
    runs-on: ubuntu-latest
    steps:
      - run: echo "No changes detected since last release. Skipping release."
