name: Build and Push Docker Image (Production)

on:
  push:
    branches:
      - main
    paths:
      - 'src/**'
      - 'client/**'
      - 'Dockerfile'
      - 'docker-compose.prod.yml'
      - 'package.json'
      - '.gitea/workflows/docker-publish-main.yaml'
  workflow_dispatch:

env:
  REGISTRY: git.app.flexinit.nl
  IMAGE_NAME: oussamadouhou/ai-stack-deployer

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

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

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: oussamadouhou
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest
            type=sha,prefix=main-

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
