version: '3.8'

services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=postgresql+asyncpg://mtgo_user:mtgo_password@postgres:5432/mtgo_platform
      - MTG_DATABASE_URL=postgresql+asyncpg://mtgo_user:mtgo_password@postgres_mtgdata:5432/mtg_data
      - REDIS_URL=redis://redis:6379/0
      - SECRET_KEY=your-secret-key-change-in-production
      - JWT_SECRET_KEY=your-jwt-secret-key-change-in-production
      - DATA_DIR=/app/data
      - UPLOAD_DIR=/app/uploads
    volumes:
      - mtg_data:/app/data
      - mtg_uploads:/app/uploads
      - mtg_logs:/app/logs
    depends_on:
      postgres:
        condition: service_healthy
      postgres_mtgdata:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 20s
      retries: 3
      start_period: 40s
    networks:
      - mtgonline_network

  postgres:
    image: postgres:16-alpine
    environment:
      - POSTGRES_USER=mtgo_user
      - POSTGRES_PASSWORD=mtgo_password
      - POSTGRES_DB=mtgo_platform
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U mtgo_user -d mtgo_platform"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - mtgonline_network

  postgres_mtgdata:
    image: postgres:16-alpine
    environment:
      - POSTGRES_USER=mtgo_user
      - POSTGRES_PASSWORD=mtgo_password
      - POSTGRES_DB=mtg_data
    volumes:
      - postgres_mtgdata:/var/lib/postgresql/data
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U mtgo_user -d mtg_data"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - mtgonline_network

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - mtgonline_network

volumes:
  postgres_data:
  postgres_mtgdata:
  mtg_data:
  mtg_uploads:
  mtg_logs:

networks:
  mtgonline_network:
    driver: bridge
