name: Build and Publish Docker Image

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

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

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

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

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels)
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=sha,prefix=
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Resolve app version
        id: app-version
        shell: bash
        run: |
          if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
            echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
          else
            echo "version=" >> "$GITHUB_OUTPUT"
          fi

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: Dockerfile.complete
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            VITE_APP_VERSION=${{ steps.app-version.outputs.version }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          platforms: linux/amd64,linux/arm64

      - name: Generate Usage Instructions
        run: |
          echo "=========================================="
          echo "Docker Image Published!"
          echo "=========================================="
          echo ""
          echo "Pull the image:"
          echo "  docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
          echo ""
          echo "Run the container:"
          echo "  docker run -d -p 80:80 \\"
          echo "    -v bilinote-data:/app/backend/data \\"
          echo "    -v bilinote-config:/app/backend/config \\"
          echo "    -v bilinote-static:/app/backend/static \\"
          echo "    -v bilinote-models:/app/backend/models \\"
          echo "    --name bilinote \\"
          echo "    ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest"
          echo ""
          echo "Access the application at: http://localhost"
          echo "=========================================="
