# Single compose file for all environments.
# Switch environments by copying the right .env file:
#   cp .env.dev .env      → dev server
#   cp .env.local .env    → local machine
#   cp .env.staging .env  → staging
#
# Commands are identical everywhere:
#   docker compose up -d --build
#   docker compose exec api alembic upgrade head
#   docker compose logs -f api
#   docker compose down

x-app-common: &app-common
  build:
    context: .
    dockerfile: Dockerfile
    target: production
  env_file:
    - .env
  environment:
    DATABASE_URL: "postgresql+psycopg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}"
    REDIS_URL: "redis://redis:6379/0"
  volumes:
    - .:/app
    - uploads_data:/app/uploads
  networks:
    - indelis_net
  restart: unless-stopped
  depends_on:
    postgres:
      condition: service_healthy
    redis:
      condition: service_healthy

services:

  # ── FastAPI Application ───────────────────────────────────────────────────
  api:
    <<: *app-common
    container_name: ${COMPOSE_PROJECT_NAME}_api
    ports:
      - "${APP_PORT:-8000}:8000"
    environment:
      - DATABASE_URL=postgresql+psycopg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      - REDIS_URL=redis://redis:6379/0
      - S3_DOCUMENTS_BUCKET=${S3_DOCUMENTS_BUCKET:-indelis-dev-uat}
      - S3_DOCUMENTS_REGION=${S3_DOCUMENTS_REGION:-us-east-1}
      - CLOUDFRONT_DOCUMENTS_URL=${CLOUDFRONT_DOCUMENTS_URL:-https://d16xufzn5inupa.cloudfront.net}
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY}
      - STRIPE_PUBLISHABLE_KEY=${STRIPE_PUBLISHABLE_KEY}
      - PORTAL_BASE_DOMAIN=${PORTAL_BASE_DOMAIN}
      - STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET}
      - STRIPE_PRICE_STARTER=${STRIPE_PRICE_STARTER}
      - STRIPE_PRICE_PROFESSIONAL=${STRIPE_PRICE_PROFESSIONAL}
      - STRIPE_PRICE_ENTERPRISE=${STRIPE_PRICE_ENTERPRISE}
      - STRIPE_TRIAL_DAYS=${STRIPE_TRIAL_DAYS:-14}
      - JWT_SECRET=${JWT_SECRET}
      - JWT_REFRESH_SECRET=${JWT_REFRESH_SECRET}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT:-587}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASSWORD=${SMTP_PASSWORD}
      - EMAIL_FROM=${EMAIL_FROM}
      - RECAPTCHA_SECRET_KEY=${RECAPTCHA_SECRET_KEY}
      - FRONTEND_URL=${FRONTEND_URL}
      - ENVIRONMENT=${ENVIRONMENT:-development}
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health/live"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # ── ARQ Background Worker ─────────────────────────────────────────────────
  worker:
    <<: *app-common
    container_name: ${COMPOSE_PROJECT_NAME}_worker
    command: arq src.worker.arq_app.WorkerSettings
    depends_on:
      api:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "redis-cli", "-h", "redis", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # ── PostgreSQL 15 + PostGIS ───────────────────────────────────────────────
  postgres:
    image: postgis/postgis:15-3.4-alpine
    container_name: ${COMPOSE_PROJECT_NAME}_postgres
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C"
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - indelis_net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 15s
    restart: unless-stopped

  # ── Redis 7 ──────────────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: ${COMPOSE_PROJECT_NAME}_redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    networks:
      - indelis_net
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    command: >
      redis-server
      --appendonly yes
      --maxmemory ${REDIS_MAXMEMORY:-256mb}
      --maxmemory-policy allkeys-lru
    restart: unless-stopped

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  uploads_data:
    driver: local

networks:
  indelis_net:
    driver: bridge
