name: Docker images

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  REGISTRY: git.shts.su

jobs:
  backend-image:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Prepare backend build context
        shell: bash
        run: |
          set -euo pipefail
          STAGING=".ci/docker/backend"
          rm -rf "$STAGING"
          mkdir -p "$STAGING/packages/contracts" "$STAGING/backend"
          cp package.json package-lock.json "$STAGING/"
          cp packages/contracts/package.json packages/contracts/tsconfig.json "$STAGING/packages/contracts/"
          cp -R packages/contracts/src "$STAGING/packages/contracts/"
          cp backend/package.json backend/tsconfig.json "$STAGING/backend/"
          cp -R backend/src "$STAGING/backend/"
          if [ -d backend/drizzle ]; then
            cp -R backend/drizzle "$STAGING/backend/"
          fi

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

      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: git.shts.su
          username: ${{ gitea.actor }}
          password: ${{ secrets.ACTIONS_PAT }}

      - name: Build and push backend image
        uses: docker/build-push-action@v5
        with:
          context: .ci/docker/backend
          file: backend/Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ gitea.repository }}:backend-latest
            ${{ env.REGISTRY }}/${{ gitea.repository }}:backend-${{ gitea.sha }}
          cache-from: type=gha,scope=backend
          cache-to: type=gha,mode=max,scope=backend
          labels: |
            org.opencontainers.image.title=mmapp-backend
            org.opencontainers.image.description=MikroTik Manager backend (Fastify)
            org.opencontainers.image.version=latest
            org.opencontainers.image.revision=${{ gitea.sha }}
            org.opencontainers.image.created=${{ gitea.event.head_commit.timestamp }}

  frontend-image:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Prepare frontend build context
        shell: bash
        run: |
          set -euo pipefail
          STAGING=".ci/docker/frontend"
          rm -rf "$STAGING"
          mkdir -p "$STAGING/packages/contracts"
          cp package-lock.json next.config.ts tsconfig.json postcss.config.mjs eslint.config.mjs components.json "$STAGING/"
          node <<'NODE'
          const fs = require("node:fs")
          const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"))
          pkg.workspaces = ["packages/*"]
          fs.writeFileSync(".ci/docker/frontend/package.json", `${JSON.stringify(pkg, null, 2)}\n`)
          NODE
          cp packages/contracts/package.json packages/contracts/tsconfig.json "$STAGING/packages/contracts/"
          cp -R packages/contracts/src "$STAGING/packages/contracts/"
          for dir in app components lib shared entities public hooks; do
            if [ -d "$dir" ]; then
              cp -R "$dir" "$STAGING/"
            fi
          done

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

      - name: Log in to Gitea Registry
        uses: docker/login-action@v3
        with:
          registry: git.shts.su
          username: ${{ gitea.actor }}
          password: ${{ secrets.ACTIONS_PAT }}

      - name: Build and push frontend image
        uses: docker/build-push-action@v5
        with:
          context: .ci/docker/frontend
          file: Dockerfile.frontend
          platforms: linux/amd64
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ gitea.repository }}:frontend-latest
            ${{ env.REGISTRY }}/${{ gitea.repository }}:frontend-${{ gitea.sha }}
          cache-from: type=gha,scope=frontend
          cache-to: type=gha,mode=max,scope=frontend
          labels: |
            org.opencontainers.image.title=mmapp-frontend
            org.opencontainers.image.description=MikroTik Manager frontend (Next.js)
            org.opencontainers.image.version=latest
            org.opencontainers.image.revision=${{ gitea.sha }}
            org.opencontainers.image.created=${{ gitea.event.head_commit.timestamp }}

  notify-webhook:
    if: ${{ secrets.DEPLOY_WEBHOOK_URL != '' }}
    needs:
      - backend-image
      - frontend-image
    runs-on: ubuntu-latest
    steps:
      - name: Notify deploy webhook
        shell: bash
        env:
          WEBHOOK_URL: ${{ secrets.DEPLOY_WEBHOOK_URL }}
        run: |
          set -euo pipefail
          curl --fail --silent --show-error --request POST \
            --header "Content-Type: application/json" \
            --data "{\"repository\":\"${{ gitea.repository }}\",\"sha\":\"${{ gitea.sha }}\",\"ref\":\"${{ gitea.ref }}\"}" \
            "$WEBHOOK_URL"
