services:
  prod-web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
      args:
        - NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY_PROD}
        - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY_PROD}
    container_name: prod-web
    ports:
      - "3001:3000"
    restart: always
    environment:
      - NEXT_PUBLIC_API_URL=https://affordablehome-ac.com/api/v1
      - API_INTERNAL_URL=http://prod-api:8000
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY_PROD}
      - STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET_PROD}
      - NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY_PROD}
    volumes:
      - ./apps/web/lib/content:/app/apps/web/lib/content
      - ./apps/web/storage/prod:/app/apps/web/storage
    depends_on:
      prod-api:
        condition: service_healthy
    healthcheck:
      test: [ "CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://127.0.0.1:3000" ]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 30s

  prod-api:
    build:
      context: ./apps/api
    container_name: prod-api
    ports:
      - "8001:8000"
    restart: always
    environment:
      - DATABASE_URL=postgresql+asyncpg://${POSTGRES_USER}:${POSTGRES_PASSWORD}@prod-db:5432/${POSTGRES_DB}
      - REDIS_URL=redis://prod-redis:6379
      - CHAOS_MODE=False
      - STRIPE_SECRET_KEY=${STRIPE_SECRET_KEY_PROD}
      - STRIPE_WEBHOOK_SECRET=${STRIPE_WEBHOOK_SECRET_PROD}
      - SMTP_SERVER=${SMTP_SERVER}
      - SMTP_PORT=${SMTP_PORT}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASSWORD=${SMTP_PASSWORD}
      - ADMIN_EMAIL=${ADMIN_EMAIL}
    volumes:
      - ./apps/api/content:/app/content
    command: uvicorn main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips "*"
    depends_on:
      prod-db:
        condition: service_healthy
      prod-redis:
        condition: service_healthy
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8000/api/v1/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 60s

  prod-db:
    image: postgres:16-alpine
    container_name: prod-db
    ports:
      - "5433:5432"
    restart: always
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - prod_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres -d ${POSTGRES_DB:-ahac_db}" ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

  prod-redis:
    image: redis:alpine
    container_name: prod-redis
    restart: always
    ports:
      - "6380:6379"
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 20s

volumes:
  prod_postgres_data:
