services:
  # PostgreSQL Database (port 5452 instead of 5432)
  postgres:
    image: postgres:16-alpine
    container_name: forge-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: forge_user
      POSTGRES_PASSWORD: forge_secure_password_2024
      POSTGRES_DB: forge_ai
    ports:
      - "5452:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./docker/init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U forge_user -d forge_ai"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis (port 6399 instead of 6379)
  redis:
    image: redis:7-alpine
    container_name: forge-redis
    restart: unless-stopped
    ports:
      - "6399:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Next.js Frontend (port 3020 instead of 3000)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: forge-frontend
    restart: unless-stopped
    ports:
      - "3020:3000"
    environment:
      - NODE_ENV=development
      - NEXT_PUBLIC_API_URL=http://localhost:8020/api/v1
      - DATABASE_URL=postgresql://forge_user:forge_secure_password_2024@postgres:5432/forge_ai
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./storage:/app/storage
      - ./frontend:/app
      - /app/node_modules
      - /app/.next
    command: npm run dev

  # FastAPI Backend (port 8020 instead of 8000)
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: forge-backend
    restart: unless-stopped
    ports:
      - "8020:8000"
    environment:
      - DATABASE_URL=postgresql://forge_user:forge_secure_password_2024@postgres:5432/forge_ai
      - REDIS_URL=redis://redis:6379
      - STORAGE_PATH=/app/storage
      - PYTHONUNBUFFERED=1
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./storage:/app/storage
      - ./backend:/app
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Celery Worker for background jobs
  worker:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: forge-worker
    restart: unless-stopped
    command: celery -A app.workers.celery_app worker --loglevel=info --concurrency=4
    environment:
      - DATABASE_URL=postgresql://forge_user:forge_secure_password_2024@postgres:5432/forge_ai
      - REDIS_URL=redis://redis:6379
      - STORAGE_PATH=/app/storage
      - PYTHONUNBUFFERED=1
    env_file:
      - .env
    depends_on:
      - backend
      - redis
    volumes:
      - ./storage:/app/storage
      - ./backend:/app

  # Nginx Reverse Proxy (port 8080 instead of 80)
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    container_name: forge-nginx
    restart: unless-stopped
    ports:
      - "8100:80"
    volumes:
      - ./storage:/var/www/storage:ro
    depends_on:
      - frontend
      - backend

volumes:
  postgres_data:
  redis_data:

networks:
  default:
    name: forge-network
