name: Website

on:
  push:
    branches:
      - master
      - main
    paths:
      - 'website/**'
      - 'assets/**'
  pull_request:
    types: [opened, synchronize, ready_for_review, reopened]
    paths:
      - 'website/**'
      - 'assets/**'

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

concurrency:
  group: "website"
  cancel-in-progress: false

defaults:
  run:
    shell: bash

jobs:
  update-date:
    name: Auto update dates
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' || (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
    steps:
      - uses: DeterminateSystems/nix-installer-action@c5a866b6ab867e88becbed4467b93592bce69f8a # v21
        with:
          diagnostic-endpoint: ""
      - name: Checkout code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v4

      - name: Check for changes
        run: |
          IS_CHANGED=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -Ec "^website*" || :)
          [[ $IS_CHANGED -gt 0 ]] && echo "IS_CHANGED=true" >> $GITHUB_ENV || echo "IS_CHANGED=false" >> $GITHUB_ENV

      - name: Update last modified date in modified docs
        if: env.IS_CHANGED == 'true'
        run: |
          git diff --name-only --diff-filter=d ${{ github.event.before }} ${{ github.sha }} | grep -E "^website*" \
          | sed -e 's/\(.*\)/"\1"/' | xargs sed -i "/date:/c\date: $(date +'%Y-%m-%d')"

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@98357b18bf14b5342f975ff684046ec3b2a07725 # v6
        if: env.IS_CHANGED == 'true' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
        with:
          commit-message: Auto-updated docs
          branch: docs-generator
          title: Auto-generated docs update
          body: |
            Auto generated docs from master commit ${{ github.sha }}

  website-generate:
    name: Auto generate website
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request' || (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
    steps:
      - uses: DeterminateSystems/nix-installer-action@c5a866b6ab867e88becbed4467b93592bce69f8a # v21
        with:
          diagnostic-endpoint: ""
      - name: Checkout code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v4
      - name: nix checks
        run: nix flake check
      - name: nix build
        env:
          HUGO_ENVIRONMENT: production
          HUGO_ENV: production
        run: nix build .#website
      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@983d7736d9b0ae728b81ab479565c72886d7745b # v5
      - name: Upload artifact
        uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v3
        with:
          path: ./result

  website-deploy:
    name: Deploy website
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: website-generate
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4
