name: Build and publish Docker image to GHCR

on:
  push:
    branches: [ "main" ]
    tags:
      - "v*"

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: |
            ghcr.io/${{ github.repository_owner }}/chess-tutor:latest
            ghcr.io/${{ github.repository_owner }}/chess-tutor:${{ github.sha }}
          # Ensure API key is not baked into the image
          build-args: |
            NEXT_PUBLIC_GEMINI_API_KEY=
