version: '3.8'

services:
  # ═══════════════════════════════════════════════════════════════
  # FRONTEND - Next.js
  # ═══════════════════════════════════════════════════════════════
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: ciccio-frontend
    restart: unless-stopped
    environment:
      - NODE_ENV=production
      - NEXT_PUBLIC_API_URL=http://backend:4000/api
    depends_on:
      - backend
    networks:
      - ciccio-net

  # ═══════════════════════════════════════════════════════════════
  # BACKEND - Fastify API
  # ═══════════════════════════════════════════════════════════════
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: ciccio-backend
    restart: unless-stopped
    env_file: .env
    environment:
      - DATABASE_URL=postgresql://cicciotrading:Cicc10Tr4d1ng!2026@postgres:5432/cicciotrading
      - REDIS_URL=redis://redis:6379
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - ciccio-net

  # ═══════════════════════════════════════════════════════════════
  # POSTGRESQL
  # ═══════════════════════════════════════════════════════════════
  postgres:
    image: postgres:16-alpine
    container_name: ciccio-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=cicciotrading
      - POSTGRES_PASSWORD=Cicc10Tr4d1ng!2026
      - POSTGRES_DB=cicciotrading
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U cicciotrading"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - ciccio-net

  # ═══════════════════════════════════════════════════════════════
  # REDIS
  # ═══════════════════════════════════════════════════════════════
  redis:
    image: redis:7-alpine
    container_name: ciccio-redis
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - ciccio-net

  # ═══════════════════════════════════════════════════════════════
  # NGINX - Reverse Proxy
  # ═══════════════════════════════════════════════════════════════
  nginx:
    image: nginx:alpine
    container_name: ciccio-nginx
    restart: unless-stopped
    ports:
      - "3028:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - frontend
      - backend
    networks:
      - ciccio-net

volumes:
  postgres_data:
  redis_data:

networks:
  ciccio-net:
    driver: bridge
