version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: serdark-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME:-serdark_db}
      POSTGRES_USER: ${DB_USER:-serdark}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-change_this_password}
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=en_US.utf8"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./backend/database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "5432:5432"
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-serdark}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache (optional but recommended)
  redis:
    image: redis:7-alpine
    container_name: serdark-redis
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD:-change_this_redis_password} --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  # Backend API
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: serdark-backend
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: 5000
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: ${DB_NAME:-serdark_db}
      DB_USER: ${DB_USER:-serdark}
      DB_PASSWORD: ${DB_PASSWORD:-change_this_password}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-change_this_redis_password}
      JWT_SECRET: ${JWT_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      CSRF_SECRET: ${CSRF_SECRET}
      FRONTEND_URL: ${FRONTEND_URL:-https://serdark.digital}
      API_URL: ${API_URL:-https://api.serdark.digital}
    volumes:
      - ./backend/logs:/app/logs
      - ./backend/uploads:/app/uploads
    ports:
      - "5000:5000"
    networks:
      - app-network
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Frontend (React + Nginx)
  frontend:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: serdark-frontend
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-network
    depends_on:
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    volumes:
      # Mount SSL certificates if available
      - ./ssl:/etc/nginx/ssl:ro

networks:
  app-network:
    driver: bridge

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