version: "3.9"

services:
  # ─── PostgreSQL ──────────────────────────────────────────────────────────
  postgres:
    image: postgres:16-alpine
    container_name: prepoc_postgres
    environment:
      POSTGRES_DB: ${DB_NAME:-prepoc_erp}
      POSTGRES_USER: ${DB_USER:-prepoc}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-prepoc_password}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-prepoc}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - prepoc_network

  # ─── Redis ───────────────────────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: prepoc_redis
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - prepoc_network

  # ─── Django Backend ───────────────────────────────────────────────────────
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: prepoc_backend
    command: >
      sh -c "python manage.py migrate --no-input &&
             python manage.py collectstatic --no-input &&
             gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 4 --timeout 120"
    volumes:
      - ./backend:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    ports:
      - "8000:8000"
    env_file:
      - .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - prepoc_network

  # ─── Celery Worker ────────────────────────────────────────────────────────
  celery_worker:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: prepoc_celery_worker
    command: celery -A config worker --loglevel=info --concurrency=4
    volumes:
      - ./backend:/app
      - media_volume:/app/media
    env_file:
      - .env
    depends_on:
      - backend
      - redis
    networks:
      - prepoc_network

  # ─── Celery Beat ──────────────────────────────────────────────────────────
  celery_beat:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: prepoc_celery_beat
    command: celery -A config beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler
    volumes:
      - ./backend:/app
    env_file:
      - .env
    depends_on:
      - backend
      - redis
    networks:
      - prepoc_network

  # ─── React Frontend (dev) ─────────────────────────────────────────────────
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: prepoc_frontend
    volumes:
      - ./frontend:/app
      - /app/node_modules
    ports:
      - "5173:5173"
    env_file:
      - .env
    networks:
      - prepoc_network

  # ─── Nginx ────────────────────────────────────────────────────────────────
  nginx:
    image: nginx:alpine
    container_name: prepoc_nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    ports:
      - "80:80"
    depends_on:
      - backend
      - frontend
    networks:
      - prepoc_network

volumes:
  postgres_data:
  redis_data:
  static_volume:
  media_volume:

networks:
  prepoc_network:
    driver: bridge
