version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: ibw_mysql_dev
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-root_password_secret}
      MYSQL_DATABASE: ${DB_NAME:-indian_books_worldwide}
      MYSQL_USER: ${DB_USER:-ibw_user}
      MYSQL_PASSWORD: ${DB_PASSWORD:-ibw_password_secret}
    ports:
      - "${DB_PORT:-3306}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: ibw_redis_dev
    restart: unless-stopped
    command: redis-server --requirepass ${REDIS_PASSWORD:-redis_password_secret}
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a $${REDIS_PASSWORD} ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 5

  meilisearch:
    image: getmeili/meilisearch:v1.6
    container_name: ibw_meilisearch_dev
    restart: unless-stopped
    environment:
      MEILI_MASTER_KEY: ${MEILI_MASTER_KEY:-meili_master_key_secret_12345}
      MEILI_ENV: development
    ports:
      - "7700:7700"
    volumes:
      - meili_data:/meili_data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7700/health"]
      interval: 10s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: .
      dockerfile: ./apps/backend/Dockerfile
    container_name: ibw_backend_dev
    restart: unless-stopped
    ports:
      - "${PORT:-5000}:5000"
    environment:
      NODE_ENV: development
      PORT: 5000
      DATABASE_URL: mysql://${DB_USER:-ibw_user}:${DB_PASSWORD:-ibw_password_secret}@mysql:3306/${DB_NAME:-indian_books_worldwide}
      DB_HOST: mysql
      DB_PORT: 3306
      DB_USER: ${DB_USER:-ibw_user}
      DB_PASSWORD: ${DB_PASSWORD:-ibw_password_secret}
      DB_NAME: ${DB_NAME:-indian_books_worldwide}
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-redis_password_secret}
      REDIS_URL: redis://:${REDIS_PASSWORD:-redis_password_secret}@redis:6379
      MEILISEARCH_HOST: http://meilisearch:7700
      MEILISEARCH_KEY: ${MEILI_MASTER_KEY:-meili_master_key_secret_12345}
      ASSET_STORAGE_DIR: /app/storage
      ASSET_PUBLIC_BASE_URL: /assets
      JWT_SECRET: ${JWT_SECRET:-super_secret_jwt_access_key_change_in_prod}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET:-super_secret_jwt_refresh_key_change_in_prod}
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
      meilisearch:
        condition: service_healthy
    volumes:
      - catalog_storage:/app/storage
      - catalog_logs:/app/apps/backend/logs/catalog-import

  frontend:
    build:
      context: .
      dockerfile: ./apps/frontend/Dockerfile
    container_name: ibw_frontend_dev
    restart: unless-stopped
    ports:
      - "${FRONTEND_PORT:-3000}:3000"
    environment:
      NODE_ENV: development
      NEXT_PUBLIC_API_URL: /api/v1
      BACKEND_INTERNAL_URL: http://backend:5000
    depends_on:
      - backend

  nginx:
    image: nginx:alpine
    container_name: ibw_nginx_dev
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - frontend
      - backend

volumes:
  mysql_data:
  redis_data:
  meili_data:
  catalog_storage:
  catalog_logs:
