name: Go Unit Tests

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

permissions:
  contents: read
  pull-requests: write

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

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod
          cache: true

      - name: Ensure embedded frontend path exists
        run: |
          mkdir -p cmd/server/frontend/dist
          printf '%s\n' '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>' > cmd/server/frontend/dist/index.html

      - name: Run unit tests and generate coverage
        run: |
          set -euo pipefail
          go test -v ./... | tee test_output.txt
          go test -v -coverprofile=coverage.out -coverpkg=./... ./tests/unit/... | tee -a test_output.txt
          go tool cover -func=coverage.out > coverage.txt
          go tool cover -html=coverage.out -o coverage.html

      - name: Parse test results and coverage summary
        if: always()
        run: |
          TESTS_PASSED=$(grep -c -- '--- PASS:' test_output.txt || true)
          TESTS_TOTAL=$(grep -c -- '=== RUN' test_output.txt || true)
          COVERAGE=$(grep '^total:' coverage.txt | awk '{print $3}' || true)
          echo "Unit tests run: ${TESTS_TOTAL:-0}" > test_summary.txt
          echo "Unit tests passed: ${TESTS_PASSED:-0}" >> test_summary.txt
          echo "Test coverage: ${COVERAGE:-0.0%}" >> test_summary.txt

      - name: Upload coverage report (PR only)
        if: always() && github.event_name == 'pull_request'
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: |
            coverage.txt
            coverage.html

      - name: Comment test summary (PR only)
        if: always() && github.event_name == 'pull_request'
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          recreate: true
          path: test_summary.txt
