name: ci

on:
  push:
    paths:
      - 'JSMR.Api/**'
      - 'JSMR.Application/**'
      - 'JSMR.Domain/**'
      - 'JSMR.Infrastructure/**'
      - 'JSMR.UI.Blazor/**'
      - 'JSMR.Tests/**'
      - '.gitea/workflows/ci.yml'
  pull_request:
    paths:
      - 'JSMR.Api/**'
      - 'JSMR.Application/**'
      - 'JSMR.Domain/**'
      - 'JSMR.Infrastructure/**'
      - 'JSMR.UI.Blazor/**'
      - 'JSMR.Tests/**'
      - '.gitea/workflows/ci.yml'

jobs:
  build-test:
    runs-on: [self-hosted, linux, x64, docker]
    container:
      image: ghcr.io/catthehacker/ubuntu:act-latest
      options: >-
        --privileged
        --add-host=host.testcontainers.internal:host-gateway
    env:
      # Testcontainers will substitute this host into GetConnectionString()
      TESTCONTAINERS_HOST_OVERRIDE: host.testcontainers.internal
    steps:
      - uses: actions/checkout@v4  
      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'          
      
      - name: Docker sanity (ensures socket mount is working)
        run: docker version
        
      - name: Install WASM workload
        run: dotnet workload install wasm-tools --skip-sign-check
        
      - run: dotnet restore
      - run: dotnet build --configuration Release --no-restore
      - run: dotnet test  --configuration Release --no-build --logger "trx;LogFileName=test-results.trx"
      
  publish-image:
      if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
      needs: build-test
      runs-on: [self-hosted, linux, x64, docker]
      container:
          image: ghcr.io/catthehacker/ubuntu:act-latest
          options: >-
            --privileged
      env:
        REGISTRY: ${{ secrets.REGISTRY_HOST }}
      steps:
        - uses: actions/checkout@v4

        - name: Set image variables
          id: vars
          shell: bash
          run: |
            OWNER_LC="$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')"
            SHA_SHORT="$(echo "${{ github.sha }}" | cut -c1-7)"
            echo "owner_lc=$OWNER_LC" >> "$GITHUB_OUTPUT"
            echo "sha_short=$SHA_SHORT" >> "$GITHUB_OUTPUT"

        - name: Docker login
          shell: bash
          run: |
            echo "${{ secrets.REGISTRY_PASSWORD }}" \
            | docker login "${{ env.REGISTRY }}" \
                -u "${{ secrets.REGISTRY_USER }}" \
                --password-stdin

        - name: Enable BuildKit
          run: echo "DOCKER_BUILDKIT=1" >> $GITHUB_ENV

        - name: Build API image
          shell: bash
          run: |
            docker build \
              -f JSMR.Api/Dockerfile \
              -t "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-api:${{ steps.vars.outputs.sha_short }}" \
              -t "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-api:main" \
              .

        - name: Push API image
          shell: bash
          run: |
            docker push "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-api:${{ steps.vars.outputs.sha_short }}"
            docker push "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-api:main"

        - name: Build Web image
          shell: bash
          run: |
            docker build \
              -f JSMR.UI.Blazor/Dockerfile \
              -t "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-web:${{ steps.vars.outputs.sha_short }}" \
              -t "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-web:main" \
              .

        - name: Push Web image
          shell: bash
          run: |
            docker push "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-web:${{ steps.vars.outputs.sha_short }}"
            docker push "${{ env.REGISTRY }}/${{ steps.vars.outputs.owner_lc }}/jsmr-web:main"