name: Deploy

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: 'pages'
  cancel-in-progress: false

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  verify-tag:
    name: Verify Tag
    runs-on: ubuntu-latest
    outputs:
      should_deploy: ${{ steps.check.outputs.should_deploy }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - name: Check if tag is on main or master branch
        id: check
        run: |
          git fetch origin main master || true
          BRANCHES=$(git branch -r --contains ${{ github.ref }})
          
          if echo "$BRANCHES" | grep -qE 'origin/(main|master)'; then
            echo "✓ Tag is on main or master branch"
            echo "should_deploy=true" >> $GITHUB_OUTPUT
          else
            echo "✗ Tag is not on main or master branch, skipping deployment"
            echo "Tag is on: $BRANCHES"
            echo "should_deploy=false" >> $GITHUB_OUTPUT
          fi
      
      - name: Verify version consistency
        if: steps.check.outputs.should_deploy == 'true'
        run: |
          # Extract version from git tag (remove 'v' prefix)
          TAG_VERSION="${{ github.ref_name }}"
          TAG_VERSION="${TAG_VERSION#v}"
          # Extract version from Cargo.toml
          CARGO_VERSION=$(grep -m1 '^version' Cargo.toml | sed 's/.*"\(.*\)"/\1/')
          echo "Git tag version: $TAG_VERSION"
          echo "Cargo.toml version: $CARGO_VERSION"
          if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
            echo "ERROR: Version mismatch! Tag version ($TAG_VERSION) != Cargo.toml version ($CARGO_VERSION)"
            exit 1
          fi
          echo "✓ Version check passed!"

  check:
    uses: ./.github/workflows/check.yml
    needs: verify-tag
    if: needs.verify-tag.outputs.should_deploy == 'true'

  test:
    uses: ./.github/workflows/test.yml
    needs: verify-tag
    if: needs.verify-tag.outputs.should_deploy == 'true'

  build:
    name: Build documentation
    runs-on: ubuntu-latest
    needs: [verify-tag, check, test]
    if: needs.verify-tag.outputs.should_deploy == 'true'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Build docs
        env:
          RUSTDOCFLAGS: -D rustdoc::broken_intra_doc_links -D missing-docs
        run: |
          # Build documentation
          cargo doc --no-deps --all-features
          
          # Auto-detect documentation directory
          # Check if doc exists in target/doc or target/*/doc
          if [ -d "target/doc" ]; then
            DOC_DIR="target/doc"
          else
            # Find doc directory under target/*/doc pattern
            DOC_DIR=$(find target -type d -name doc -path "target/*/doc" | head -n 1)
            if [ -z "$DOC_DIR" ]; then
              echo "Error: Could not find documentation directory"
              exit 1
            fi
          fi
          
          echo "Documentation found in: $DOC_DIR"
          printf '<meta http-equiv="refresh" content="0;url=%s/index.html">' $(cargo tree | head -1 | cut -d' ' -f1) > "${DOC_DIR}/index.html"
          echo "DOC_DIR=${DOC_DIR}" >> $GITHUB_ENV

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ${{ env.DOC_DIR }}

  deploy:
    name: Deploy to GitHub Pages
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: [verify-tag, build]
    if: needs.verify-tag.outputs.should_deploy == 'true'
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
