name: ci

on:
  push:
    branches:
      - main
      - staging
      - dev
    tags:
      - '*'

jobs:
  docker:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    strategy:
      matrix:
        include:
          - service: frontend
            context: frontend
            dockerfile: frontend/Dockerfile
          - service: backend
            context: backend
            dockerfile: backend/Dockerfile

    steps:
      - name: Checkout with submodules
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Derive repository metadata
        id: repo_meta
        run: |
          repo="${GITHUB_REPOSITORY}"
          owner="${repo%%/*}"
          name="${repo##*/}"
          echo "repo_owner=$owner" >> "$GITHUB_OUTPUT"
          echo "repo_name=$name" >> "$GITHUB_OUTPUT"

      - name: Compute base tag
        id: compute_tag
        env:
          GITHUB_SHA: ${{ github.sha }}
          GITHUB_REF_TYPE: ${{ github.ref_type }}
          GITHUB_REF_NAME: ${{ github.ref_name }}
        run: |
          sha="${GITHUB_SHA}"
          ref_type="${GITHUB_REF_TYPE}"
          ref_name="${GITHUB_REF_NAME}"

          short="${sha:0:7}"
          tag="$short"

          if [ "$ref_type" = "tag" ]; then
            tag="$ref_name"
          elif [ "$ref_name" = "dev" ]; then
            tag="${tag}-dev"
          elif [ "$ref_name" = "staging" ]; then
            tag="${tag}-staging"
          fi

          echo "base_tag=$tag" >> "$GITHUB_OUTPUT"

      - name: Login to local registry
        if: ${{ vars.REGISTRY_URL != '' }}
        uses: docker/login-action@v2
        with:
          registry: ${{ vars.REGISTRY_URL }}
          username: ${{ vars.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Login to GHCR
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ github.token }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

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

      - name: Determine branch alias tag
        id: branch_alias
        env:
          REF_NAME: ${{ github.ref_name }}
        run: |
          alias=""
          case "${REF_NAME}" in
            dev) alias="latest-dev" ;;
            staging) alias="latest-staging" ;;
            main) alias="latest" ;;
          esac
          echo "alias=$alias" >> "$GITHUB_OUTPUT"

      - name: Assemble image tags
        id: tag_list
        env:
          REGISTRY_URL: ${{ vars.REGISTRY_URL }}
          REPO_NAME: ${{ steps.repo_meta.outputs.repo_name }}
          SERVICE: ${{ matrix.service }}
          BASE_TAG: ${{ steps.compute_tag.outputs.base_tag }}
          GIT_SHA: ${{ github.sha }}
          BRANCH_ALIAS: ${{ steps.branch_alias.outputs.alias }}
        shell: bash
        run: |
          tags=""

          if [ -n "${REGISTRY_URL}" ]; then
            repo_tag="${REGISTRY_URL}/${REPO_NAME}-${SERVICE}"
            tags="${tags}${repo_tag}:${BASE_TAG}\n${repo_tag}:${GIT_SHA}"
          fi

          ghcr_tag="ghcr.io/papercrate-dms/${REPO_NAME}-${SERVICE}"
          if [ -n "${tags}" ]; then
            tags="${tags}\n"
          fi
          tags="${tags}${ghcr_tag}:${BASE_TAG}\n${ghcr_tag}:${GIT_SHA}"

          if [ -n "${BRANCH_ALIAS}" ]; then
            if [ -n "${REGISTRY_URL}" ]; then
              tags="${tags}\n${repo_tag}:${BRANCH_ALIAS}"
            fi
            tags="${tags}\n${ghcr_tag}:${BRANCH_ALIAS}"
          fi

          export TAGS="${tags}"

          python -c 'import os; tags=[t.strip() for t in os.environ["TAGS"].split("\\n") if t.strip()]; print("tags=" + ",".join(tags))' | tee -a "$GITHUB_OUTPUT"

      - name: Build and Push ${{ matrix.service }} Image
        uses: docker/build-push-action@v6
        with:
          context: ${{ matrix.context }}
          file: ${{ matrix.dockerfile }}
          platforms: linux/amd64,linux/arm64
          push: true
          provenance: false
          tags: ${{ steps.tag_list.outputs.tags }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
