name: CI

on:
  push:
    branches:
      - next
  pull_request:
    branches:
      - next

jobs:
  test:
    name: Test Suite
    runs-on: ubuntu-latest

    # Service containers for database, Redis, and Minio
    services:
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: plunk_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

      redis:
        image: redis:7-alpine
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 6379:6379

      minio:
        image: minio/minio:edge-cicd
        env:
          MINIO_ROOT_USER: plunk
          MINIO_ROOT_PASSWORD: plunkminiopass
        ports:
          - 9000:9000
        options: >-
          --health-cmd "curl -f http://localhost:9000/minio/health/live || exit 1"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

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

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

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Setup environment variables
        run: |
          cat > .env << EOF
          NODE_ENV=test

          # Database
          DATABASE_URL=postgresql://postgres:postgres@localhost:5432/plunk_test
          DIRECT_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/plunk_test

          # Redis
          REDIS_URL=redis://localhost:6379

          # Security
          JWT_SECRET=test-jwt-secret-for-ci-only

          # S3 (Minio)
          S3_ENDPOINT=http://localhost:9000
          S3_ACCESS_KEY_ID=plunk
          S3_ACCESS_KEY_SECRET=plunkminiopass
          S3_BUCKET=uploads
          S3_PUBLIC_URL=http://localhost:9000/uploads
          S3_FORCE_PATH_STYLE=true

          # Application URLs (not needed for tests but required by schema)
          API_URI=http://localhost:8080
          DASHBOARD_URI=http://localhost:3000
          LANDING_URI=http://localhost:4000
          WIKI_URI=http://localhost:1000

          # AWS SES (mock values for tests)
          AWS_SES_REGION=us-east-1
          AWS_SES_ACCESS_KEY_ID=mock
          AWS_SES_SECRET_ACCESS_KEY=mock
          SES_CONFIGURATION_SET=test
          SES_CONFIGURATION_SET_NO_TRACKING=test-no-tracking
          EOF

      - name: Build shared packages
        run: yarn build --filter="@plunk/db" --filter="@plunk/types" --filter="@plunk/shared"

      - name: Generate Prisma Client
        run: yarn workspace @plunk/db db:generate
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost:5432/plunk_test
          DIRECT_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/plunk_test

      - name: Run database migrations
        run: yarn workspace @plunk/db migrate:prod
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost:5432/plunk_test
          DIRECT_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/plunk_test

      - name: Create MinIO bucket
        run: |
          docker run --rm --network host \
            --entrypoint /bin/sh minio/mc:latest \
            -c "mc alias set local http://localhost:9000 plunk plunkminiopass && \
                mc mb local/uploads --ignore-existing"

      - name: Run tests
        run: yarn test:run
        env:
          NODE_ENV: test

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: |
            coverage/
            **/*.test.ts.log
          retention-days: 7

  lint:
    name: Lint & Type Check
    runs-on: ubuntu-latest

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

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

      - name: Install dependencies
        run: yarn install --frozen-lockfile

      - name: Setup minimal env (for build)
        run: |
          cat > .env << EOF
          NODE_ENV=development
          DATABASE_URL=postgresql://postgres:postgres@localhost:5432/plunk
          DIRECT_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/plunk
          REDIS_URL=redis://localhost:6379
          JWT_SECRET=test

          # S3 (minimal - not needed but schema may require)
          S3_ENDPOINT=http://localhost:9000
          S3_ACCESS_KEY_ID=mock
          S3_ACCESS_KEY_SECRET=mock
          S3_BUCKET=uploads
          S3_PUBLIC_URL=http://localhost:9000/uploads
          S3_FORCE_PATH_STYLE=true

          # Application URLs
          API_URI=http://localhost:8080
          DASHBOARD_URI=http://localhost:3000
          LANDING_URI=http://localhost:4000
          WIKI_URI=http://localhost:1000

          # AWS SES (mock)
          AWS_SES_REGION=us-east-1
          AWS_SES_ACCESS_KEY_ID=mock
          AWS_SES_SECRET_ACCESS_KEY=mock
          SES_CONFIGURATION_SET=test
          SES_CONFIGURATION_SET_NO_TRACKING=test
          EOF

      - name: Build shared packages
        run: yarn build --filter="@plunk/db" --filter="@plunk/types" --filter="@plunk/shared"

      - name: Run linter
        run: yarn lint

      - name: Type check
        run: yarn build --filter="api" --filter="web" --filter="landing" --filter="wiki" || true
