services:
  db:
    # Pulled via the Docker Hub mirror (mirror.gcr.io) to avoid
    # registry-1.docker.io timeouts. Revert to postgres:16-alpine if needed.
    image: mirror.gcr.io/library/postgres:16-alpine
    container_name: ctt_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-ctt}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-ctt_password}
      POSTGRES_DB: ${POSTGRES_DB:-ctt}
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-ctt} -d ${POSTGRES_DB:-ctt}"]
      interval: 5s
      timeout: 5s
      retries: 10
    # Exposed only for local debugging; remove in production.
    ports:
      - "5432:5432"

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: ctt_backend
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-ctt}:${POSTGRES_PASSWORD:-ctt_password}@db:5432/${POSTGRES_DB:-ctt}
      JWT_SECRET: ${JWT_SECRET:-change-me-in-production}
      JWT_ALGORITHM: ${JWT_ALGORITHM:-HS256}
      ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-43200}
      SCRAPER_ENABLED: ${SCRAPER_ENABLED:-true}
      SCRAPER_INTERVAL_MINUTES: ${SCRAPER_INTERVAL_MINUTES:-360}
      SCHEDULE_TIMEZONE: ${SCHEDULE_TIMEZONE:-Europe/London}
      CORS_ORIGINS: ${CORS_ORIGINS:-http://localhost:8080,http://localhost:5173}
      # AI bolt-on (optional). Leave the key blank to keep AI disabled.
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-}
      AI_MODEL: ${AI_MODEL:-claude-haiku-4-5}
      # Outgoing email (optional). Leave SMTP_HOST blank to hide email features.
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USERNAME: ${SMTP_USERNAME:-}
      SMTP_PASSWORD: ${SMTP_PASSWORD:-}
      SMTP_FROM: ${SMTP_FROM:-}
      SMTP_STARTTLS: ${SMTP_STARTTLS:-true}
    ports:
      - "8000:8000"

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        # Same-origin "/api" path, reverse-proxied to the backend by nginx.
        # Works on the Docker host AND from remote machines (no localhost dependency).
        VITE_API_BASE_URL: ${VITE_API_BASE_URL:-/api}
    container_name: ctt_frontend
    restart: unless-stopped
    depends_on:
      - backend
    ports:
      - "8080:80"

volumes:
  pgdata:
    driver: local
