name: Publish Container

on:
  push:
    branches:
      - master
  workflow_dispatch:

env:
  DOTNET_VERSION: 10.0.x
  REGISTRY: ${{ vars.FORGEJO_REGISTRY }}
  IMAGE_NAMESPACE: ${{ vars.IMAGE_NAMESPACE }}
  IMAGE_NAME: ${{ vars.IMAGE_NAME }}

jobs:
  publish:
    runs-on: docker
    env:
      # Use explicit override when provided, otherwise use the known-good host
      DOCKER_HOST: ${{ vars.DOCKER_HOST != '' && vars.DOCKER_HOST || 'tcp://172.17.0.1:2375' }}

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

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Validate workflow variables
        run: |
          set -eu
          if [ -z "${REGISTRY}" ]; then echo "vars.FORGEJO_REGISTRY is required"; exit 1; fi
          if [ -z "${IMAGE_NAMESPACE}" ]; then echo "vars.IMAGE_NAMESPACE is required"; exit 1; fi
          if [ -z "${IMAGE_NAME}" ]; then echo "vars.IMAGE_NAME is required"; exit 1; fi

      - name: Validate registry secrets
        run: |
          set -eu
          if [ -z "${{ secrets.FORGEJO_REGISTRY_USERNAME }}" ]; then echo "secrets.FORGEJO_REGISTRY_USERNAME is required"; exit 1; fi
          if [ -z "${{ secrets.FORGEJO_REGISTRY_TOKEN }}" ]; then echo "secrets.FORGEJO_REGISTRY_TOKEN is required"; exit 1; fi

      - name: Ensure Docker CLI exists
        run: |
          set -eu
          if command -v docker >/dev/null 2>&1; then
            docker --version
            exit 0
          fi

          ARCH="$(uname -m)"
          case "${ARCH}" in
            x86_64) DOCKER_ARCH="x86_64" ;;
            aarch64|arm64) DOCKER_ARCH="aarch64" ;;
            *) echo "Unsupported architecture for Docker CLI bootstrap: ${ARCH}"; exit 1 ;;
          esac

          DOCKER_CLI_VERSION="27.5.1"
          curl -fsSL "https://download.docker.com/linux/static/stable/${DOCKER_ARCH}/docker-${DOCKER_CLI_VERSION}.tgz" -o docker.tgz
          tar -xzf docker.tgz
          mkdir -p "${HOME}/.local/bin"
          mv docker/docker "${HOME}/.local/bin/docker"
          chmod +x "${HOME}/.local/bin/docker"
          echo "${HOME}/.local/bin" >> "${GITHUB_PATH}"
          "${HOME}/.local/bin/docker" --version

      - name: Ensure Docker Buildx exists
        run: |
          set -eu
          if docker buildx version >/dev/null 2>&1; then
            docker buildx version
            exit 0
          fi

          ARCH="$(uname -m)"
          case "${ARCH}" in
            x86_64) BUILDX_ARCH="amd64" ;;
            aarch64|arm64) BUILDX_ARCH="arm64" ;;
            *) echo "Unsupported architecture for Docker Buildx bootstrap: ${ARCH}"; exit 1 ;;
          esac

          BUILDX_VERSION="v0.21.1"
          mkdir -p "${HOME}/.docker/cli-plugins"
          curl -fsSL "https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/buildx-${BUILDX_VERSION}.linux-${BUILDX_ARCH}" -o "${HOME}/.docker/cli-plugins/docker-buildx"
          chmod +x "${HOME}/.docker/cli-plugins/docker-buildx"
          docker buildx version

      # Simplified: we trust DOCKER_HOST (default to tcp://172.17.0.1:2375). If you
      # need a different endpoint, set the Forgejo variable `DOCKER_HOST`.

      - name: Check Docker daemon connectivity
        run: |
          set -eu
          echo "Using DOCKER_HOST=${DOCKER_HOST}"
          docker version
          docker info >/dev/null

      - name: Create Buildx builder
        run: |
          set -eu
          docker buildx rm forgejo-builder >/dev/null 2>&1 || true
          docker buildx create --name forgejo-builder --driver docker-container --use
          docker buildx inspect --bootstrap

      - name: Restore and publish app
        run: dotnet publish WorkTracker.csproj -c Release -o ./artifacts/publish

      - name: Login to Forgejo registry
        run: |
          echo "${{ secrets.FORGEJO_REGISTRY_TOKEN }}" | docker login "${REGISTRY}" -u "${{ secrets.FORGEJO_REGISTRY_USERNAME }}" --password-stdin

      - name: Build and push image
        run: |
          set -eu
          IMAGE_REF="${REGISTRY}/${IMAGE_NAMESPACE}/${IMAGE_NAME}"
          SHORT_SHA="$(echo "${GITHUB_SHA}" | cut -c1-12)"
          docker buildx build \
            --builder forgejo-builder \
            --tag "${IMAGE_REF}:sha-${SHORT_SHA}" \
            --tag "${IMAGE_REF}:latest" \
            --push \
            .
