# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Docs Build and Deploy

on:
  push:
    branches:
      - main
    paths:
      - ".github/workflows/docs.yml"
      - "requirements-docs.txt"
      - "mkdocs.yml"
      - "docs/**"
  pull_request:
    branches:
      - main
    paths:
      - ".github/workflows/docs.yml"
      - "requirements-docs.txt"
      - "mkdocs.yml"
      - "docs/**"

jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      actions: read

    if: github.repository == 'google/A2UI'

    steps:
      - name: Checkout Code
        uses: actions/checkout@v5
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Configure Git Credentials
        run: |
          git config --global user.name github-actions[bot]
          git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: 3.13

      - name: Restore pip cache
        uses: actions/cache@v4
        with:
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements-docs.txt') }}
          path: ~/.cache/pip
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install documentation dependencies
        run: pip install -r requirements-docs.txt

      - name: Build Documentation (PR Check)
        if: github.event_name == 'pull_request'
        run: mkdocs build

      - name: Deploy development version from main branch
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        run: |
          mkdocs gh-deploy
