version: '3.9'

# ============================================================
# BiznesOutlet.pl – Docker Compose (produkcja)
# Uruchom: docker compose up -d
# ============================================================

services:

  # ── PostgreSQL ──────────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    container_name: biznesoutlet_db
    restart: unless-stopped
    environment:
      POSTGRES_DB: biznesoutlet
      POSTGRES_USER: biznesoutlet_user
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./sql/schema.sql:/docker-entrypoint-initdb.d/01_schema.sql:ro
    ports:
      - "127.0.0.1:5432:5432"   # tylko localhost
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U biznesoutlet_user -d biznesoutlet"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ── MinIO (S3-compatible storage) ───────────────────────────
  minio:
    image: minio/minio:latest
    container_name: biznesoutlet_minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${S3_ACCESS_KEY}
      MINIO_ROOT_PASSWORD: ${S3_SECRET_KEY}
    volumes:
      - minio_data:/data
    ports:
      - "127.0.0.1:9000:9000"   # API
      - "127.0.0.1:9001:9001"   # Console (tylko dev)
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ── Node.js API ─────────────────────────────────────────────
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: biznesoutlet_api
    restart: unless-stopped
    env_file: .env
    environment:
      DB_HOST: postgres
      S3_ENDPOINT: http://minio:9000
    depends_on:
      postgres:
        condition: service_healthy
      minio:
        condition: service_healthy
    ports:
      - "127.0.0.1:3001:3001"
    volumes:
      - ./logs:/app/logs
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3001/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ── Nginx (reverse proxy + SSL) ─────────────────────────────
  nginx:
    image: nginx:alpine
    container_name: biznesoutlet_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro          # certbot certs
      - /var/www/html:/var/www/html:ro          # frontend static files
    depends_on:
      - api

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