name: Build and Deploy

on:
  push:
    branches: [main]
    tags: ['v*']
  workflow_dispatch:

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

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: false

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

      - name: Enable Corepack
        run: corepack enable

      - uses: actions/setup-node@v6
        with:
          cache: yarn
          cache-dependency-path: yarn.lock

      - run: yarn install --immutable

      - run: yarn turbo run build --filter=ciabatta-frontend

      - name: Upload pages artifact
        if: startsWith(github.ref, 'refs/tags/')
        uses: actions/upload-pages-artifact@v5
        with:
          path: frontend/dist

  deploy:
    needs: build
    # Allow tags and master
    if: startsWith(github.ref, 'refs/tags/')
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Setup Pages
        uses: actions/configure-pages@v6

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5
