name: Build frontend Docker image

on:
  push:
    branches:
      - release
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          sparse-checkout: |
            app

      - name: Extract version from package.json
        id: version
        run: |
          VERSION=$(jq -r '.version' package.json)
          echo "version=$VERSION" >> $GITEA_OUTPUT
          echo "Extracted version: $VERSION"

      - name: Extract registry URL without protocol
        id: registry
        run: |
          # Remove https:// or http:// prefix
          SERVER_URL="${{ gitea.server_url }}"
          REGISTRY_URL="${SERVER_URL#https://}"
          REGISTRY_URL="${REGISTRY_URL#http://}"
          echo "registry_url=$REGISTRY_URL" >> $GITEA_OUTPUT
          echo "Registry URL: $REGISTRY_URL"

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

      - name: Log in to Gitea Container Registry
        uses: docker/login-action@v4
        with:
          registry: ${{ gitea.server_url }}
          username: ${{ gitea.actor }}
          password: ${{ secrets.REGISTRY_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v7
        with:
          push: true
          tags: |
            ${{ steps.registry.outputs.registry_url }}/${{ gitea.repository }}-frontend:${{ steps.version.outputs.version }}
            ${{ steps.registry.outputs.registry_url }}/${{ gitea.repository }}-frontend:latest
          build-args: |
            NUXT_PUBLIC_APP_URL=${{ vars.NUXT_PUBLIC_APP_URL }}
            NUXT_PUBLIC_POCKETBASE_URL=${{ vars.NUXT_PUBLIC_POCKETBASE_URL }}
