version: '3.8'

# ============================================
# Docker Compose pour développement local Mac
# ============================================
# Usage:
#   docker-compose -f docker-compose.local.yml up -d
#   docker-compose -f docker-compose.local.yml logs -f backend
#   docker-compose -f docker-compose.local.yml down

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: xpeditis-postgres-local
    restart: unless-stopped
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: xpeditis_dev
      POSTGRES_USER: xpeditis
      POSTGRES_PASSWORD: xpeditis_dev_password
      PGDATA: /var/lib/postgresql/data/pgdata
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U xpeditis"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: xpeditis-redis-local
    restart: unless-stopped
    ports:
      - "6379:6379"
    command: redis-server --requirepass xpeditis_redis_password --appendonly yes
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # MinIO S3 Storage
  minio:
    image: minio/minio:latest
    container_name: xpeditis-minio-local
    restart: unless-stopped
    ports:
      - "9000:9000"  # API
      - "9001:9001"  # Console
    command: server /data --console-address ":9001"
    volumes:
      - minio_data:/data
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  # Backend API (NestJS) - Image depuis Scaleway Registry
  backend:
    image: rg.fr-par.scw.cloud/weworkstudio/xpeditis-backend:preprod
    container_name: xpeditis-backend-local
    restart: unless-stopped
    ports:
      - "4000:4000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_started
    environment:
      NODE_ENV: development
      PORT: 4000

      # Database
      DATABASE_HOST: postgres
      DATABASE_PORT: 5432
      DATABASE_USER: xpeditis
      DATABASE_PASSWORD: xpeditis_dev_password
      DATABASE_NAME: xpeditis_dev

      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: xpeditis_redis_password

      # JWT
      JWT_SECRET: dev-secret-key-change-in-production
      JWT_ACCESS_EXPIRATION: 15m
      JWT_REFRESH_EXPIRATION: 7d

      # S3/MinIO
      AWS_S3_ENDPOINT: http://minio:9000
      AWS_REGION: us-east-1
      AWS_ACCESS_KEY_ID: minioadmin
      AWS_SECRET_ACCESS_KEY: minioadmin
      AWS_S3_BUCKET: xpeditis-csv-rates

      # CORS
      CORS_ORIGIN: http://localhost:3000,http://localhost:3001

      # App URLs
      FRONTEND_URL: http://localhost:3000
      API_URL: http://localhost:4000

    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://localhost:4000/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # Frontend (Next.js) - Image depuis Scaleway Registry
  frontend:
    image: rg.fr-par.scw.cloud/weworkstudio/xpeditis-frontend:preprod
    container_name: xpeditis-frontend-local
    restart: unless-stopped
    ports:
      - "3000:3000"
    depends_on:
      - backend
    environment:
      NODE_ENV: development
      NEXT_PUBLIC_API_URL: http://localhost:4000
      NEXT_PUBLIC_WS_URL: ws://localhost:4000
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

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