name: Test CV Site

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

jobs:
  test:
    name: Test on Go ${{ matrix.go-version }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: ['1.25.1']

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
          cache: true

      - name: Install dependencies
        run: go mod download

      - name: Verify dependencies
        run: go mod verify

      - name: Run linter
        uses: golangci/golangci-lint-action@v7
        with:
          version: v2.6.0

      - name: Run tests with coverage
        run: |
          go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...

      - name: Generate coverage report
        run: |
          go tool cover -func=coverage.txt | tee coverage-report.txt

      - name: Check coverage threshold
        run: |
          coverage=$(go tool cover -func=coverage.txt | grep total | awk '{print $3}' | sed 's/%//')
          echo "Total coverage: ${coverage}%"
          echo "COVERAGE=${coverage}" >> $GITHUB_ENV
          if (( $(echo "$coverage < 70" | bc -l) )); then
            echo "⚠️  Coverage ${coverage}% is below target of 70%"
            echo "This is a warning, not a failure (building towards 70% coverage)"
          else
            echo "✅ Coverage ${coverage}% meets or exceeds target"
          fi

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        with:
          files: ./coverage.txt
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: false

      - name: Run benchmarks
        run: |
          go test -bench=. -benchmem ./... | tee benchmark.txt

      - name: Build binary
        run: |
          go build -v -o cv-server .

      - name: Upload test artifacts
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results-go-${{ matrix.go-version }}
          path: |
            coverage.txt
            coverage-report.txt
            benchmark.txt
          retention-days: 30

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: cv-server-binary
          path: cv-server
          retention-days: 7
