name: ci

on:
  push:
    paths:
      - 'JSMR.Api/**'
      - 'JSMR.Application/**'
      - 'JSMR.Domain/**'
      - 'JSMR.Infrastructure/**'
      - 'JSMR.Tests/**'
  pull_request:
    paths:
      - 'JSMR.Api/**'
      - 'JSMR.Application/**'
      - 'JSMR.Domain/**'
      - 'JSMR.Infrastructure/**'
      - 'JSMR.Tests/**'

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: '9.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: ${{ false }}   # disabled for now
      runs-on: [self-hosted, linux, x64, docker]
      container:
          image: ghcr.io/catthehacker/ubuntu:act-latest
          options: >-
            --privileged
      env:
        REGISTRY: ${{ secrets.REGISTRY_HOST }}
        OWNER_REPO: ${{ github.repository }}
        DOCKERFILE: JSMR.Api/Dockerfile
        CONTEXT: .
      steps:
        - uses: actions/checkout@v4

        - name: Normalize image name (lowercase)
          id: names
          run: |
            IMAGE_LC="$(echo "${REGISTRY}/${OWNER_REPO}" | tr '[:upper:]' '[:lower:]')"
            echo "image=${IMAGE_LC}" >> "$GITHUB_OUTPUT"

        - name: Docker login
          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
          run: docker build -f "$DOCKERFILE" -t "${{ steps.names.outputs.image }}:${{ github.sha }}" "$CONTEXT"

        - name: Push
          run: |
            docker push "${{ steps.names.outputs.image }}:${{ github.sha }}"
            docker tag "${{ steps.names.outputs.image }}:${{ github.sha }}" "${{ steps.names.outputs.image }}:latest"
            docker push "${{ steps.names.outputs.image }}:latest"