name: CI Pipeline

on:
  push:
    branches: ["main", "develop", "feature/**"]
    paths-ignore:
      - "**.md"
      - "docs/**"
      - ".gitignore"
      - "LICENSE"
  pull_request:
    branches: ["main"]
    paths-ignore:
      - "**.md"
      - "docs/**"
      - ".gitignore"
      - "LICENSE"
  workflow_dispatch:

jobs:
  backend-ci:
    name: Backend Build & Test
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup .NET 10
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Restore NuGet cache
        uses: actions/cache@v4
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('backend/**/*.csproj') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      - name: Restore dependencies
        working-directory: ./backend
        run: dotnet restore WorkClub.slnx

      - name: Build solution
        working-directory: ./backend
        run: dotnet build WorkClub.slnx --configuration Release --no-restore

      - name: Run unit tests
        working-directory: ./backend
        run: dotnet test WorkClub.Tests.Unit/WorkClub.Tests.Unit.csproj --configuration Release --no-build --verbosity normal --logger "trx;LogFileName=unit-tests.trx"

      - name: Run integration tests
        working-directory: ./backend
        run: dotnet test WorkClub.Tests.Integration/WorkClub.Tests.Integration.csproj --configuration Release --no-build --verbosity normal --logger "trx;LogFileName=integration-tests.trx"

      - name: Upload test results on failure
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: backend-test-results
          path: backend/**/TestResults/*.trx
          retention-days: 7

  frontend-ci:
    name: Frontend Lint, Test & Build
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.x'

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Restore Bun cache
        uses: actions/cache@v4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-${{ hashFiles('frontend/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-

      - name: Install dependencies
        working-directory: ./frontend
        run: bun install --frozen-lockfile

      - name: Run linter
        working-directory: ./frontend
        run: bun run lint

      - name: Install jsdom for Vitest
        working-directory: ./frontend
        run: bun add -d jsdom

      - name: Run unit tests
        working-directory: ./frontend
        run: bun run test

      - name: Build Next.js application
        working-directory: ./frontend
        run: bun run build
        env:
          NEXT_PUBLIC_API_URL: "http://localhost:5001"
          NEXTAUTH_URL: "http://localhost:3000"
          NEXTAUTH_SECRET: "ci-build-secret-not-used-at-runtime"
          KEYCLOAK_CLIENT_ID: "workclub-app"
          KEYCLOAK_CLIENT_SECRET: "ci-build-secret"
          KEYCLOAK_ISSUER: "http://localhost:8080/realms/workclub"

      - name: Upload build artifacts on failure
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: frontend-build-logs
          path: |
            frontend/.next/
            frontend/out/
          retention-days: 7

  infra-ci:
    name: Infrastructure Validation
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Validate docker-compose.yml
        run: docker compose config --quiet

      - name: Install Kustomize
        run: |
          curl -Lo kustomize.tar.gz https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.1/kustomize_v5.4.1_linux_amd64.tar.gz
          tar -xzf kustomize.tar.gz
          chmod +x kustomize
          sudo mv kustomize /usr/local/bin/
          kustomize version

      - name: Validate kustomize base
        working-directory: ./infra/k8s
        run: kustomize build base > /dev/null

      - name: Validate kustomize dev overlay
        working-directory: ./infra/k8s
        run: kustomize build overlays/dev > /dev/null

      - name: Upload validation errors on failure
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: infra-validation-errors
          path: |
            docker-compose.yml
            infra/k8s/**/*.yaml
          retention-days: 7
