version: '3.9'

services:
  # ── PostgreSQL ─────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    container_name: ketobay_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-ketobay}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB:-ketobay}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./infrastructure/db-backup:/backups
    ports:
      - "127.0.0.1:5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-ketobay}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── Redis ──────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: ketobay_redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD}
    volumes:
      - redis_data:/data
    ports:
      - "127.0.0.1:6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s

  # ── Ollama (AI) ────────────────────────────────────────
  ollama:
    image: ollama/ollama:latest
    container_name: ketobay_ollama
    restart: unless-stopped
    volumes:
      - ollama_data:/root/.ollama
    ports:
      - "127.0.0.1:11434:11434"
    # Uncomment for GPU support:
    # deploy:
    #   resources:
    #     reservations:
    #       devices:
    #         - driver: nvidia
    #           count: 1
    #           capabilities: [gpu]

  # ── MinIO (file storage) ───────────────────────────────
  minio:
    image: minio/minio:latest
    container_name: ketobay_minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_USER:-ketobay}
      MINIO_ROOT_PASSWORD: ${MINIO_PASSWORD}
    volumes:
      - minio_data:/data
    ports:
      - "127.0.0.1:9000:9000"
      - "127.0.0.1:9001:9001"

  # ── API Backend ────────────────────────────────────────
  api:
    build:
      context: .
      dockerfile: apps/api/Dockerfile
    container_name: ketobay_api
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      NODE_ENV: production
      DATABASE_URL: postgresql://${POSTGRES_USER:-ketobay}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-ketobay}
      REDIS_URL: redis://:${REDIS_PASSWORD}@redis:6379
      JWT_SECRET: ${JWT_SECRET}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
      STRIPE_PRICE_BASIC: ${STRIPE_PRICE_BASIC}
      STRIPE_PRICE_PRO: ${STRIPE_PRICE_PRO}
      STRIPE_PRICE_LIFETIME: ${STRIPE_PRICE_LIFETIME}
      OLLAMA_URL: http://ollama:11434
      OLLAMA_MODEL: ${OLLAMA_MODEL:-llama3:8b}
      FRONTEND_URL: ${FRONTEND_URL:-https://theketobay.com}
      SMTP_HOST: ${SMTP_HOST}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USER: ${SMTP_USER}
      SMTP_PASS: ${SMTP_PASS}
      SMTP_FROM: ${SMTP_FROM:-noreply@theketobay.com}
      MINIO_ENDPOINT: http://minio:9000
      MINIO_ACCESS_KEY: ${MINIO_USER:-ketobay}
      MINIO_SECRET_KEY: ${MINIO_PASSWORD}
    ports:
      - "127.0.0.1:3001:3001"

  # ── Next.js Frontend ───────────────────────────────────
  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
    container_name: ketobay_web
    restart: unless-stopped
    depends_on:
      - api
    environment:
      NEXT_PUBLIC_API_URL: http://api:3001
      NEXT_PUBLIC_SITE_URL: ${FRONTEND_URL:-https://theketobay.com}
    ports:
      - "127.0.0.1:3000:3000"

  # ── Telegram Bot ───────────────────────────────────────
  bot:
    build:
      context: .
      dockerfile: apps/bot/Dockerfile
    container_name: ketobay_bot
    restart: unless-stopped
    depends_on:
      - api
    environment:
      TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
      FRONTEND_URL: ${FRONTEND_URL:-https://theketobay.com}
      API_URL: http://api:3001

  # ── Nginx Reverse Proxy ────────────────────────────────
  nginx:
    image: nginx:alpine
    container_name: ketobay_nginx
    restart: unless-stopped
    depends_on:
      - web
      - api
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./infrastructure/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./infrastructure/nginx/conf.d:/etc/nginx/conf.d:ro
      - certbot_www:/var/www/certbot:ro
      - certbot_conf:/etc/letsencrypt:ro

  # ── Certbot (SSL) ──────────────────────────────────────
  certbot:
    image: certbot/certbot:latest
    container_name: ketobay_certbot
    volumes:
      - certbot_www:/var/www/certbot
      - certbot_conf:/etc/letsencrypt
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"

  # ── Auto DB backup ─────────────────────────────────────
  db-backup:
    image: postgres:16-alpine
    container_name: ketobay_backup
    restart: unless-stopped
    depends_on:
      - postgres
    environment:
      PGPASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - ./infrastructure/db-backup:/backups
    command: >
      sh -c "while true; do
        pg_dump -h postgres -U ${POSTGRES_USER:-ketobay} ${POSTGRES_DB:-ketobay} > /backups/backup_$$(date +%Y%m%d_%H%M%S).sql;
        find /backups -name '*.sql' -mtime +7 -delete;
        sleep 86400;
      done"

volumes:
  postgres_data:
  redis_data:
  ollama_data:
  minio_data:
  certbot_www:
  certbot_conf:
