# Aurora CI/CD Pipeline
# Builds, tests, and deploys to production server

name: Deploy to Production

on:
  push:
    branches: [main]
  workflow_dispatch:  # Allow manual trigger

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  # ==========================================================================
  # Test Job
  # ==========================================================================
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:17-alpine
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: aurora_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

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

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

      - name: Install Dependencies
        run: |
          bun install --frozen-lockfile
          cd web && bun install --frozen-lockfile

      - name: Create Config File
        run: |
          mkdir -p shared/config
          cat <<EOF > shared/config/config.json
          {
              "leveling": { "base": 100, "exponent": 2.5, "chat": { "cooldownMs": 60000, "minXp": 15, "maxXp": 25 } },
              "economy": {
                  "daily": { "amount": "100", "streakBonus": "10", "weeklyBonus": "50", "cooldownMs": 86400000 },
                  "transfers": { "allowSelfTransfer": false, "minAmount": "1" },
                  "exam": { "multMin": 0.05, "multMax": 0.03 }
              },
              "inventory": { "maxStackSize": "99", "maxSlots": 50 },
              "commands": {},
              "lootdrop": {
                  "activityWindowMs": 120000, "minMessages": 1, "spawnChance": 1, "cooldownMs": 3000,
                  "reward": { "min": 40, "max": 150, "currency": "Astral Units" }
              },
              "studentRole": "123", "visitorRole": "456", "colorRoles": [],
              "moderation": {
                  "prune": { "maxAmount": 100, "confirmThreshold": 50, "batchSize": 100, "batchDelayMs": 1000 },
                  "cases": { "dmOnWarn": false }
              },
              "trivia": {
                  "entryFee": "50", "rewardMultiplier": 1.5, "timeoutSeconds": 30, "cooldownMs": 60000,
                  "categories": [], "difficulty": "random"
              },
              "system": {}
          }
          EOF

      - name: Setup Test Database
        run: bun run db:push:local
        env:
          DATABASE_URL: postgresql://postgres:postgres@postgres:5432/aurora_test
          # Create .env.test for implicit usage by bun
          DISCORD_BOT_TOKEN: test_token
          DISCORD_CLIENT_ID: 123
          DISCORD_GUILD_ID: 123

      - name: Run Tests
        run: |
          # Create .env.test for test-sequential.sh / bun test
          cat <<EOF > .env.test
          DATABASE_URL="postgresql://postgres:postgres@postgres:5432/aurora_test"
          DISCORD_BOT_TOKEN="test_token"
          DISCORD_CLIENT_ID="123456789"
          DISCORD_GUILD_ID="123456789"
          ADMIN_TOKEN="admin_token_123"
          LOG_LEVEL="error"
          EOF
          bash shared/scripts/test-sequential.sh
        env:
          NODE_ENV: test
