name: Generate Changelog Report

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      from_tag:
        description: 'Start tag'
        required: false
      to_tag:
        description: 'End tag (default: latest)'
        required: false

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

jobs:
  generate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Generate Report
        uses: ./
        id: report
        with:
          ai_provider: gemini
          ai_model: gemini-2.5-flash
          ai_api_key: ${{ secrets.AI_API_KEY }}
          task_file: tasks/changelog_report.yml
        env:
          FROM_TAG: ${{ inputs.from_tag || '' }}
          TO_TAG: ${{ inputs.to_tag || github.event.release.tag_name || 'HEAD' }}
          REPORT_TITLE: "Cicaddy Action — Release Update"

      - name: Upload report artifact
        uses: actions/upload-artifact@v7
        with:
          name: changelog-report
          path: ${{ steps.report.outputs.report_html }}

      - name: Prepare Pages
        run: |
          mkdir -p _site
          cp "${{ steps.report.outputs.report_html }}" _site/index.html

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: _site

  deploy:
    needs: generate
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
