name: Deploy Documentation

on:
  push:
    tags:
      - "v*.*.*"

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Build documentation
        run: |
          cargo doc --no-deps --document-private-items --all-features
          # Create a simple index.html that redirects to the main documentation
          cat > target/doc/index.html << 'EOF'
          <!DOCTYPE html>
          <html>
          <head>
              <meta charset="utf-8">
              <title>DS Common Logger Rust Library</title>
              <meta http-equiv="refresh" content="0; url=ds_common_logger_rs_lib/index.html">
          </head>
          <body>
              <p>Redirecting to <a href="ds_common_logger_rs_lib/index.html">DS Common Logger Rust Library Documentation</a>...</p>
          </body>
          </html>
          EOF

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: target/doc

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