name: Build and Publish Docker Image

on:
  # Trigger on releases
  release:
    types: [published]

  # Trigger on commits to master and develop branches
  push:
    branches:
      - master
      - develop

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

jobs:
  build-and-push:
    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: 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: |
            # Tag as 'latest' for releases
            type=raw,value=latest,enable=${{ github.event_name == 'release' }}
            # Tag with version for releases (e.g., v1.0.0, 1.0.0, 1.0, 1)
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            # Tag as 'edge' for master branch commits
            type=raw,value=edge,enable=${{ github.ref == 'refs/heads/master' && github.event_name == 'push' }}
            # Tag with commit SHA for master branch commits
            type=sha,prefix={{branch}}-,enable=${{ github.ref == 'refs/heads/master' }}
            # Tag as 'dev' for develop branch commits
            type=raw,value=dev,enable=${{ github.ref == 'refs/heads/develop' && github.event_name == 'push' }}
            # Tag with commit SHA for develop branch commits
            type=sha,prefix={{branch}}-,enable=${{ github.ref == 'refs/heads/develop' }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

  build-and-push-linesync:
    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: 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 }}-linesync
          tags: |
            # Tag as 'latest' for releases
            type=raw,value=latest,enable=${{ github.event_name == 'release' }}
            # Tag with version for releases
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            # Tag as 'edge' for master branch commits
            type=raw,value=edge,enable=${{ github.ref == 'refs/heads/master' && github.event_name == 'push' }}
            # Tag with commit SHA for master branch commits
            type=sha,prefix={{branch}}-,enable=${{ github.ref == 'refs/heads/master' }}
            # Tag as 'dev' for develop branch commits
            type=raw,value=dev,enable=${{ github.ref == 'refs/heads/develop' && github.event_name == 'push' }}
            # Tag with commit SHA for develop branch commits
            type=sha,prefix={{branch}}-,enable=${{ github.ref == 'refs/heads/develop' }}

      - name: Build and push Linesync Docker image
        uses: docker/build-push-action@v5
        with:
          context: ./tools/linesync
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
