services:
  # PostgreSQL database for e2e tests
  db:
    image: postgres:15-alpine
    environment:
      - POSTGRES_DB=gridpilot_e2e
      - POSTGRES_USER=gridpilot_e2e_user
      - POSTGRES_PASSWORD=gridpilot_e2e_pass
    ports:
      - "5434:5432" # Different port to avoid conflicts
    volumes:
      - e2e_db_data:/var/lib/postgresql/data
    networks:
      - gridpilot-e2e-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U gridpilot_e2e_user -d gridpilot_e2e"]
      interval: 2s
      timeout: 2s
      retries: 10
      start_period: 5s
    restart: "no"

  # API server with TypeORM/PostgreSQL
  api:
    image: node:20-alpine
    working_dir: /app/apps/api
    environment:
      - NODE_ENV=test
      - PORT=3000
      - GRIDPILOT_API_PERSISTENCE=postgres
      - GRIDPILOT_API_BOOTSTRAP=true
      - GRIDPILOT_API_FORCE_RESEED=true
      - DATABASE_URL=postgres://gridpilot_e2e_user:gridpilot_e2e_pass@db:5432/gridpilot_e2e
      - POSTGRES_DB=gridpilot_e2e
      - POSTGRES_USER=gridpilot_e2e_user
      - POSTGRES_PASSWORD=gridpilot_e2e_pass
    ports:
      - "3101:3000"
    volumes:
      - ./:/app
      - /Users/marcmintel/Projects/gridpilot/node_modules:/app/node_modules:ro
    command: ["sh", "-lc", "echo '[api] Starting real API with TypeORM...'; npm run start:dev"]
    depends_on:
      db:
        condition: service_healthy
    networks:
      - gridpilot-e2e-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 2s
      timeout: 2s
      retries: 30
      start_period: 10s

  # Website server (Next.js) - fully containerized
  website:
    image: gridpilot-website-e2e
    build:
      context: .
      dockerfile: apps/website/Dockerfile.e2e
      args:
        - NODE_ENV=test
        - NEXT_PUBLIC_API_BASE_URL=http://api:3000
    working_dir: /app/apps/website
    environment:
      - NODE_ENV=test
      - NEXT_TELEMETRY_DISABLED=1
      - NEXT_PUBLIC_API_BASE_URL=http://api:3000
      - API_BASE_URL=http://api:3000
      - PORT=3000
    ports:
      - "3100:3000"
    depends_on:
      api:
        condition: service_healthy
    networks:
      - gridpilot-e2e-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 10s
      timeout: 10s
      retries: 20
      start_period: 60s

  # Playwright test runner
  playwright:
    image: mcr.microsoft.com/playwright:v1.57.0-jammy
    working_dir: /app
    environment:
      - PLAYWRIGHT_BASE_URL=http://website:3000
      - API_BASE_URL=http://api:3000
      - PLAYWRIGHT_SKIP_DOCKER_CHECK=true
      - CI=true
    volumes:
      - ./:/app
      - /Users/marcmintel/Projects/gridpilot/node_modules:/app/node_modules:ro
      # Playwright reports
      - ./test-results:/app/test-results
      - ./playwright-report:/app/playwright-report
    depends_on:
      website:
        condition: service_healthy
    command: ["sh", "-lc", "echo '[playwright] Running e2e tests...'; npx playwright test -c playwright.website.config.ts"]
    networks:
      - gridpilot-e2e-network
    restart: "no"

networks:
  gridpilot-e2e-network:
    driver: bridge

volumes:
  e2e_db_data: