services:
  # Base de données PostgreSQL
  database:
    image: postgres:17
    container_name: ptitspas-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - ./database/BDD.sql:/docker-entrypoint-initdb.d/01_init.sql
      - postgres_data:/var/lib/postgresql/data
    networks:
      - ptitspas_network

  # Interface d'administration DB
  pgadmin:
    image: dpage/pgadmin4
    container_name: ptitspas-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
      SCRIPT_NAME: /pgadmin
    depends_on:
      - database
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.ptitspas-pgadmin.rule=Host(\"app.ptits-pas.fr\") && PathPrefix(\"/pgadmin\")"
      - "traefik.http.routers.ptitspas-pgadmin.entrypoints=websecure"
      - "traefik.http.routers.ptitspas-pgadmin.tls.certresolver=leresolver"
      - "traefik.http.routers.ptitspas-pgadmin.priority=30"
      - "traefik.http.services.ptitspas-pgadmin.loadbalancer.server.port=80"
    networks:
      - ptitspas_network
      - proxy_network

  # Backend NestJS
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: ptitspas-backend
    restart: unless-stopped
    environment:
      POSTGRES_HOST: ${POSTGRES_HOST}
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      API_PORT: ${API_PORT}
      JWT_ACCESS_SECRET: ${JWT_ACCESS_SECRET}
      JWT_ACCESS_EXPIRES: ${JWT_ACCESS_EXPIRES}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      JWT_REFRESH_EXPIRES: ${JWT_REFRESH_EXPIRES}
      NODE_ENV: ${NODE_ENV}
      LOG_API_REQUESTS: ${LOG_API_REQUESTS:-false}
      CONFIG_ENCRYPTION_KEY: ${CONFIG_ENCRYPTION_KEY}
      # Photos inscription (base64) — chemin absolu dans le conteneur (voir volume backend_uploads)
      UPLOAD_PHOTOS_DIR: ${UPLOAD_PHOTOS_DIR:-/app/uploads/photos}
    volumes:
      - backend_uploads:/app/uploads/photos
      # PDF CGU / privacy (upload admin) — persistance entre redémarrages
      - backend_documents_legaux:/app/documents/legaux
    depends_on:
      - database
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.ptitspas-api.rule=Host(\"app.ptits-pas.fr\") && PathPrefix(\"/api\")"
      - "traefik.http.routers.ptitspas-api.entrypoints=websecure"
      - "traefik.http.routers.ptitspas-api.tls.certresolver=leresolver"
      - "traefik.http.routers.ptitspas-api.priority=20"
      - "traefik.http.services.ptitspas-api.loadbalancer.server.port=3000"
      # Fichiers statiques photos (URL relatives /uploads/...) — priorité > front (10)
      - "traefik.http.routers.ptitspas-uploads.rule=Host(\"app.ptits-pas.fr\") && PathPrefix(\"/uploads\")"
      - "traefik.http.routers.ptitspas-uploads.entrypoints=websecure"
      - "traefik.http.routers.ptitspas-uploads.tls.certresolver=leresolver"
      - "traefik.http.routers.ptitspas-uploads.priority=18"
      - "traefik.http.routers.ptitspas-uploads.service=ptitspas-api"
    networks:
      - ptitspas_network
      - proxy_network

  # Frontend Flutter
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: ptitspas-frontend
    restart: unless-stopped
    environment:
      API_URL: ${API_URL}
    depends_on:
      - backend
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.ptitspas-front.rule=Host(\"app.ptits-pas.fr\")"
      - "traefik.http.routers.ptitspas-front.entrypoints=websecure"
      - "traefik.http.routers.ptitspas-front.tls.certresolver=leresolver"
      - "traefik.http.routers.ptitspas-front.priority=10"
      - "traefik.http.services.ptitspas-front.loadbalancer.server.port=80"
    networks:
      - ptitspas_network
      - proxy_network

volumes:
  postgres_data:
  backend_uploads:
  backend_documents_legaux:

networks:
  ptitspas_network:
    driver: bridge
  proxy_network:
    external: true
