# ============================================================
# SocialAI Platform — Full-Stack Docker Compose
# Services: postgres, redis, backend (Laravel), frontend (Next.js), nginx
# ============================================================

version: '3.9'

services:

  # ── PostgreSQL ───────────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    container_name: socialai_postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB:       ${DB_DATABASE:-socialai}
      POSTGRES_USER:     ${DB_USERNAME:-socialai}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-secret}
      PGDATA:            /var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-socialai} -d ${DB_DATABASE:-socialai}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - socialai_network

  # ── Redis ────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: socialai_redis
    restart: unless-stopped
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-redissecret}
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redissecret}", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - socialai_network

  # ── Laravel Backend ──────────────────────────────────────────
  backend:
    build:
      context: .
      dockerfile: Dockerfile.backend
      target: production
    container_name: socialai_backend
    restart: unless-stopped
    env_file:
      - ./backend/.env
    environment:
      APP_ENV:            production
      APP_KEY:            ${APP_KEY}
      APP_URL:            ${APP_URL:-https://api.ai.3onent.com}
      DB_CONNECTION:      pgsql
      DB_HOST:            postgres
      DB_PORT:            5432
      DB_DATABASE:        ${DB_DATABASE:-socialai}
      DB_USERNAME:        ${DB_USERNAME:-socialai}
      DB_PASSWORD:        ${DB_PASSWORD:-secret}
      REDIS_HOST:         redis
      REDIS_PORT:         6379
      REDIS_PASSWORD:     ${REDIS_PASSWORD:-redissecret}
      CACHE_STORE:        redis
      SESSION_DRIVER:     redis
      QUEUE_CONNECTION:   redis
      BROADCAST_DRIVER:   redis
    volumes:
      - backend_storage:/var/www/html/storage
      - backend_logs:/var/www/html/storage/logs
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - socialai_network
    healthcheck:
      test: ["CMD-SHELL", "php-fpm -t 2>&1 | grep -q 'successful'"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ── Laravel Horizon (Queue Worker) ───────────────────────────
  horizon:
    build:
      context: .
      dockerfile: Dockerfile.backend
      target: production
    container_name: socialai_horizon
    restart: unless-stopped
    command: ["php", "artisan", "horizon"]
    env_file:
      - ./backend/.env
    environment:
      APP_ENV:        production
      DB_HOST:        postgres
      REDIS_HOST:     redis
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redissecret}
    volumes:
      - backend_storage:/var/www/html/storage
      - backend_logs:/var/www/html/storage/logs
    depends_on:
      - backend
    networks:
      - socialai_network

  # ── Laravel Scheduler ────────────────────────────────────────
  scheduler:
    build:
      context: .
      dockerfile: Dockerfile.backend
      target: production
    container_name: socialai_scheduler
    restart: unless-stopped
    command: >
      sh -c "while true; do
        php artisan schedule:run --verbose --no-interaction;
        sleep 60;
      done"
    env_file:
      - ./backend/.env
    environment:
      APP_ENV:    production
      DB_HOST:    postgres
      REDIS_HOST: redis
    volumes:
      - backend_storage:/var/www/html/storage
    depends_on:
      - backend
    networks:
      - socialai_network

  # ── Next.js Frontend ─────────────────────────────────────────
  frontend:
    build:
      context: .
      dockerfile: Dockerfile.frontend
      target: runner
      args:
        NEXT_PUBLIC_API_URL:         ${NEXT_PUBLIC_API_URL:-https://api.ai.3onent.com/api}
        NEXT_PUBLIC_APP_URL:         ${NEXT_PUBLIC_APP_URL:-https://ai.3onent.com}
        NEXT_PUBLIC_PAYPAL_CLIENT_ID: ${NEXT_PUBLIC_PAYPAL_CLIENT_ID}
    container_name: socialai_frontend
    restart: unless-stopped
    environment:
      PORT:     3000
      HOSTNAME: 0.0.0.0
    depends_on:
      - backend
    networks:
      - socialai_network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ── Nginx Reverse Proxy ──────────────────────────────────────
  nginx:
    image: nginx:1.25-alpine
    container_name: socialai_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - nginx_logs:/var/log/nginx
      - certbot_www:/var/www/certbot:ro
      - certbot_conf:/etc/letsencrypt:ro
    depends_on:
      - backend
      - frontend
    networks:
      - socialai_network

  # ── Certbot (Let's Encrypt SSL) ───────────────────────────────
  certbot:
    image: certbot/certbot:latest
    container_name: socialai_certbot
    volumes:
      - certbot_www:/var/www/certbot
      - certbot_conf:/etc/letsencrypt
    entrypoint: >
      sh -c "trap exit TERM;
        while :; do
          certbot renew --webroot -w /var/www/certbot --quiet;
          sleep 12h & wait $${!};
        done"
    networks:
      - socialai_network

# ── Volumes ──────────────────────────────────────────────────
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  backend_storage:
    driver: local
  backend_logs:
    driver: local
  nginx_logs:
    driver: local
  certbot_www:
    driver: local
  certbot_conf:
    driver: local

# ── Networks ─────────────────────────────────────────────────
networks:
  socialai_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16