version: '3.9'

services:
  # ---------------------------------------------------------------------------
  # Infrastructure
  # ---------------------------------------------------------------------------
  mysql:
    image: mysql:8.0
    container_name: idp-mysql
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-idp_root_password}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-idp_db}
      MYSQL_USER: ${MYSQL_USER:-idp_user}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-idp_password}
    ports:
      - '13306:3306'
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ['CMD', 'mysqladmin', 'ping', '-h', '127.0.0.1', '-uroot', '-p${MYSQL_ROOT_PASSWORD:-idp_root_password}']
      interval: 10s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    container_name: idp-redis
    restart: unless-stopped
    ports:
      - '16379:6379'
    volumes:
      - redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 10s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio:latest
    container_name: idp-minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ACCESS_KEY:-idp_minio}
      MINIO_ROOT_PASSWORD: ${MINIO_SECRET_KEY:-idp_minio_secret}
    ports:
      - '19000:9000'
      - '19001:9001'
    volumes:
      - minio_data:/data
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:9000/minio/health/live']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  meilisearch:
    image: getmeili/meilisearch:v1.11
    container_name: idp-meilisearch
    restart: unless-stopped
    profiles:
      - search
    environment:
      MEILI_MASTER_KEY: ${MEILISEARCH_API_KEY:-idp_meili_master_key}
      MEILI_ENV: development
    ports:
      - '17700:7700'
    volumes:
      - meilisearch_data:/meili_data
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:7700/health']
      interval: 30s
      timeout: 10s
      retries: 3

  # ---------------------------------------------------------------------------
  # Application (Laravel API + Next.js frontend)
  # ---------------------------------------------------------------------------
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    container_name: idp-api
    restart: unless-stopped
    env_file:
      - .env
    environment:
      APP_ENV: local
      APP_DEBUG: 'true'
      APP_URL: http://localhost:3001
      FRONTEND_URL: http://localhost:3000
      DB_CONNECTION: mysql
      DB_HOST: mysql
      DB_PORT: 3306
      DB_DATABASE: ${MYSQL_DATABASE:-idp_db}
      DB_USERNAME: ${MYSQL_USER:-idp_user}
      DB_PASSWORD: ${MYSQL_PASSWORD:-idp_password}
      JWT_SECRET: ${JWT_SECRET:-mir_local_jwt_secret_replace_with_openssl_rand_hex_32}
    ports:
      - '3001:8000'
    depends_on:
      mysql:
        condition: service_healthy
    volumes:
      - ./api/storage:/app/storage

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        BACKEND_INTERNAL_URL: http://api:8000/api
        NEXT_PUBLIC_API_URL: /api
    container_name: idp-frontend
    restart: unless-stopped
    env_file:
      - .env
    environment:
      BACKEND_INTERNAL_URL: http://api:8000/api
    ports:
      - '3000:3000'
    depends_on:
      - api

  nginx:
    image: nginx:alpine
    container_name: idp-nginx
    restart: unless-stopped
    ports:
      - '${NGINX_PORT:-80}:80'
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - frontend
      - api

  # ---------------------------------------------------------------------------
  # Analytics (Advanced - optional)
  # ---------------------------------------------------------------------------
  metabase:
    image: metabase/metabase:latest
    container_name: idp-metabase
    restart: unless-stopped
    profiles:
      - advanced
    environment:
      MB_DB_TYPE: mysql
      MB_DB_DBNAME: ${MYSQL_DATABASE:-idp_db}
      MB_DB_PORT: 3306
      MB_DB_USER: ${MYSQL_USER:-idp_user}
      MB_DB_PASS: ${MYSQL_PASSWORD:-idp_password}
      MB_DB_HOST: mysql
    ports:
      - '3002:3000'
    depends_on:
      mysql:
        condition: service_healthy

volumes:
  mysql_data:
  redis_data:
  minio_data:
  meilisearch_data:
