name: PR Checks - Run

# This workflow runs advisory PR checks with read-only permissions
# Safe for fork PRs - results are saved as artifacts
# Companion workflow (pr-checks-comment.yml) will post comments
#
# NOTE: This workflow provides ADVISORY checks (non-blocking)
#       Main blocking checks are in pr-checks.yml
#       PR title and size checks are handled by pr-checks.yml (no duplication)

on:
  pull_request:
    types: [opened, synchronize, reopened]
    branches: [main, dev]

# Read-only permissions - safe for fork PRs
permissions:
  contents: read

jobs:
  # Backend advisory checks
  # Different from pr-checks.yml: these use continue-on-error and generate reports
  backend-checks:
    name: Backend Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.21'

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libta-lib-dev || true
          go mod download || true

      - name: Check Go formatting
        id: go-fmt
        continue-on-error: true
        run: |
          UNFORMATTED=$(gofmt -l . 2>/dev/null || echo "")
          if [ -n "$UNFORMATTED" ]; then
            echo "status=⚠️ Needs formatting" >> $GITHUB_OUTPUT
            echo "$UNFORMATTED" | head -10 > fmt-files.txt
          else
            echo "status=✅ Good" >> $GITHUB_OUTPUT
            echo "" > fmt-files.txt
          fi

      - name: Run go vet
        id: go-vet
        continue-on-error: true
        run: |
          if go vet ./... 2>&1 | tee vet-output.txt; then
            echo "status=✅ Good" >> $GITHUB_OUTPUT
          else
            echo "status=⚠️ Issues found" >> $GITHUB_OUTPUT
            cat vet-output.txt | head -20 > vet-output-short.txt
          fi

      - name: Run tests
        id: go-test
        continue-on-error: true
        run: |
          if go test ./... -v 2>&1 | tee test-output.txt; then
            echo "status=✅ Passed" >> $GITHUB_OUTPUT
          else
            echo "status=⚠️ Failed" >> $GITHUB_OUTPUT
            cat test-output.txt | tail -30 > test-output-short.txt
          fi

      - name: Save backend results
        if: always()
        run: |
          cat > backend-results.json <<EOF
          {
            "pr_number": ${{ github.event.pull_request.number }},
            "fmt_status": "${{ steps.go-fmt.outputs.status }}",
            "vet_status": "${{ steps.go-vet.outputs.status }}",
            "test_status": "${{ steps.go-test.outputs.status }}"
          }
          EOF

      - name: Upload backend results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: backend-results
          path: |
            backend-results.json
            fmt-files.txt
            vet-output-short.txt
            test-output-short.txt
          retention-days: 1

  # Frontend advisory checks
  # Different from pr-checks.yml: these use continue-on-error and generate reports
  frontend-checks:
    name: Frontend Checks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Check if web directory exists
        id: check-web
        run: |
          if [ -d "web" ]; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Install dependencies
        if: steps.check-web.outputs.exists == 'true'
        working-directory: ./web
        continue-on-error: true
        run: npm ci

      - name: Build and Type Check
        if: steps.check-web.outputs.exists == 'true'
        id: build
        working-directory: ./web
        continue-on-error: true
        run: |
          # build script includes: tsc && vite build
          if npm run build 2>&1 | tee build-output.txt; then
            echo "status=✅ Success" >> $GITHUB_OUTPUT
          else
            echo "status=⚠️ Failed" >> $GITHUB_OUTPUT
            cat build-output.txt | tail -30 > build-output-short.txt
          fi

      - name: Save frontend results
        if: always() && steps.check-web.outputs.exists == 'true'
        working-directory: ./web
        run: |
          cat > frontend-results.json <<EOF
          {
            "pr_number": ${{ github.event.pull_request.number }},
            "build_status": "${{ steps.build.outputs.status }}"
          }
          EOF

      - name: Upload frontend results
        if: always() && steps.check-web.outputs.exists == 'true'
        uses: actions/upload-artifact@v4
        with:
          name: frontend-results
          path: |
            web/frontend-results.json
            web/build-output-short.txt
          retention-days: 1
