services:
  db:
    image: postgres:15-alpine
    container_name: marathon-db
    environment:
      POSTGRES_USER: marathon
      POSTGRES_PASSWORD: ${DB_PASSWORD:-marathon}
      POSTGRES_DB: marathon
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5433:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U marathon"]
      interval: 5s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: marathon-backend
    environment:
      DATABASE_URL: postgresql+asyncpg://marathon:${DB_PASSWORD:-marathon}@db:5432/marathon
      SECRET_KEY: ${SECRET_KEY:-change-me-in-production}
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
      TELEGRAM_BOT_USERNAME: ${TELEGRAM_BOT_USERNAME:-BCMarathonbot}
      BOT_API_SECRET: ${BOT_API_SECRET:-}
      DEBUG: ${DEBUG:-false}
      RATE_LIMIT_ENABLED: ${RATE_LIMIT_ENABLED:-true}
      # S3 Storage
      S3_ENABLED: ${S3_ENABLED:-false}
      S3_BUCKET_NAME: ${S3_BUCKET_NAME:-}
      S3_REGION: ${S3_REGION:-ru-1}
      S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-}
      S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-}
      S3_ENDPOINT_URL: ${S3_ENDPOINT_URL:-}
      S3_PUBLIC_URL: ${S3_PUBLIC_URL:-}
    volumes:
      - ./backend/uploads:/app/uploads
      - ./backend/app:/app/app
    ports:
      - "8002:8000"
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        VITE_API_URL: ${VITE_API_URL:-/api/v1}
    container_name: marathon-frontend
    ports:
      - "3002:80"
    depends_on:
      - backend
    restart: unless-stopped

  nginx:
    image: nginx:alpine
    container_name: marathon-nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./backend/uploads:/app/uploads:ro
    depends_on:
      - frontend
      - backend
    restart: unless-stopped

  bot:
    build:
      context: ./bot
      dockerfile: Dockerfile
    container_name: marathon-bot
    environment:
      - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN}
      - API_URL=http://backend:8000
      - BOT_API_SECRET=${BOT_API_SECRET:-}
    depends_on:
      - backend
    restart: unless-stopped

  status:
    build:
      context: ./status-service
      dockerfile: Dockerfile
    container_name: marathon-status
    environment:
      BACKEND_URL: http://backend:8000
      FRONTEND_URL: http://frontend:80
      BOT_URL: http://bot:8080
      EXTERNAL_URL: ${EXTERNAL_URL:-}
      PUBLIC_URL: ${PUBLIC_URL:-}
      CHECK_INTERVAL: "30"
      TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
      TELEGRAM_ADMIN_ID: ${TELEGRAM_ADMIN_ID:-947392854}
    volumes:
      - status_data:/app/data
    ports:
      - "8001:8001"
    depends_on:
      - backend
      - frontend
      - bot
    restart: unless-stopped

  backup:
    build:
      context: ./backup-service
      dockerfile: Dockerfile
    container_name: marathon-backup
    environment:
      DB_HOST: db
      DB_PORT: "5432"
      DB_NAME: marathon
      DB_USER: marathon
      DB_PASSWORD: ${DB_PASSWORD:-marathon}
      S3_BUCKET_NAME: ${S3_BUCKET_NAME:-}
      S3_REGION: ${S3_REGION:-ru-1}
      S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:-}
      S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-}
      S3_ENDPOINT_URL: ${S3_ENDPOINT_URL:-}
      S3_BACKUP_PREFIX: ${S3_BACKUP_PREFIX:-backups/}
      TELEGRAM_BOT_TOKEN: ${TELEGRAM_BOT_TOKEN}
      TELEGRAM_ADMIN_ID: ${TELEGRAM_ADMIN_ID:-947392854}
      BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-14}
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

volumes:
  postgres_data:
  status_data:
