services:
  # Traefik pour gérer le routing local (sans SSL)
  traefik:
    image: traefik:v2.11
    container_name: faildaily_traefik_local
    group_add:
      - "1001"  # GID du groupe docker WSL2 - nécessaire pour lire /var/run/docker.sock
    environment:
      - DOCKER_API_VERSION=1.44  # Docker Desktop 4.62+ MinAPIVersion=1.44, Traefik v3 négocierait v1.24 sinon
    command:
      - "--api.dashboard=true"
      - "--providers.docker=true"
      - "--providers.docker.endpoint=unix:///var/run/docker.sock"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.docker.network=docker_faildaily-network"
      - "--entrypoints.web.address=:80"
    ports:
      - "8090:80"
      # Le dashboard Traefik n'est plus exposé publiquement (suppression --api.insecure)
      # Accès uniquement via: docker exec faildaily_traefik_local wget -O- http://localhost:8080/dashboard/
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    networks:
      - faildaily-network
    restart: unless-stopped
  
  frontend:
    build:
      context: ../
      dockerfile: docker/frontend.Dockerfile
      args:
        BUILD_CMD: build:docker:dev
    container_name: faildaily_frontend_local
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.faildaily-frontend.rule=Host(`localhost`)"
      - "traefik.http.routers.faildaily-frontend.entrypoints=web"
      - "traefik.http.routers.faildaily-frontend.priority=50"
      - "traefik.http.services.faildaily-frontend.loadbalancer.server.port=80"
    depends_on:
      - backend
    networks:
      - faildaily-network

  admin_frontend:
    build:
      context: ../
      dockerfile: docker/admin-frontend.Dockerfile
    container_name: faildaily_admin_frontend_local
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.faildaily-admin-frontend.rule=Host(`admin.localhost`)"
      - "traefik.http.routers.faildaily-admin-frontend.entrypoints=web"
      - "traefik.http.routers.faildaily-admin-frontend.priority=60"
      - "traefik.http.services.faildaily-admin-frontend.loadbalancer.server.port=80"
    depends_on:
      - backend
    networks:
      - faildaily-network

  backend:
    build:
      context: ../backend-api
      dockerfile: Dockerfile
    container_name: faildaily_backend_local
    restart: unless-stopped
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.faildaily-backend-api.rule=(Host(`localhost`) || Host(`admin.localhost`)) && PathPrefix(`/api`)"
      - "traefik.http.routers.faildaily-backend-api.entrypoints=web"
      - "traefik.http.routers.faildaily-backend-api.priority=100"
      - "traefik.http.routers.faildaily-backend-api.service=faildaily-backend-svc"
      - "traefik.http.routers.faildaily-backend-uploads.rule=(Host(`localhost`) || Host(`admin.localhost`)) && PathPrefix(`/uploads`)"
      - "traefik.http.routers.faildaily-backend-uploads.entrypoints=web"
      - "traefik.http.routers.faildaily-backend-uploads.priority=90"
      - "traefik.http.routers.faildaily-backend-uploads.service=faildaily-backend-svc"
      - "traefik.http.routers.faildaily-backend-socketio.rule=(Host(`localhost`) || Host(`admin.localhost`)) && PathPrefix(`/socket.io`)"
      - "traefik.http.routers.faildaily-backend-socketio.entrypoints=web"
      - "traefik.http.routers.faildaily-backend-socketio.priority=95"
      - "traefik.http.routers.faildaily-backend-socketio.service=faildaily-backend-svc"
      - "traefik.http.services.faildaily-backend-svc.loadbalancer.server.port=3000"
    env_file:
      - .env.local
    environment:
      - NODE_ENV=development
      - DB_NAME=faildaily_bdd
      - LOGS_DB_NAME=faildaily_bdd
    ports:
      - "3010:3000"  # Port exposé pour debug
    volumes:
      - faildaily_uploads_local:/app/uploads
    depends_on:
      - db
      - logs_db
    networks:
      - faildaily-network

  db:
    image: mysql:8.0
    container_name: faildaily_db_local
    restart: unless-stopped
    env_file:
      - .env.local
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=faildaily_bdd
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    ports:
      - "3316:3306"  # Port MySQL exposé
    volumes:
      - faildaily_db_data_local:/var/lib/mysql
      - ../backend-api/migrations/faildaily_bdd.sql:/docker-entrypoint-initdb.d/init.sql:ro
    networks:
      - faildaily-network

  # Base de données logs (séparée)
  logs_db:
    image: mysql:8.0
    container_name: faildaily_logs_db_local
    restart: unless-stopped
    env_file:
      - .env.local
    environment:
      - MYSQL_ROOT_PASSWORD=${LOGS_DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=faildaily_logs
      - MYSQL_USER=logs_user
      - MYSQL_PASSWORD=${LOGS_DB_PASSWORD}
    ports:
      - "3317:3306"  # Port exposé pour logs DB
    volumes:
      - faildaily_logs_data_local:/var/lib/mysql
      - ./init-logs-database.sql:/docker-entrypoint-initdb.d/init-logs-database.sql:ro
    networks:
      - faildaily-network

  phpmyadmin:
    image: phpmyadmin:5-apache
    container_name: faildaily_phpmyadmin_local
    restart: unless-stopped
    depends_on:
      - db
    environment:
      - PMA_HOST=faildaily_db_local
      - PMA_PORT=3306
      - PMA_ARBITRARY=0
      - UPLOAD_LIMIT=512M
    ports:
      - "8081:80"
    networks:
      - faildaily-network

volumes:
  faildaily_db_data_local:
  faildaily_logs_data_local:
  faildaily_uploads_local:

networks:
  faildaily-network:
    driver: bridge
