# Amar Room - Docker Compose
#
# Creates: db, redis, backend, celery, celery-beat, frontend
# Network: Joins external network defined by NETWORK_NAME
#
# Usage:
#   cd cfg/amar && docker compose up -d
#
# Required Environment Variables (from .env):
# - DEPLOYMENT_NAME: Prefix for container names
# - NETWORK_NAME: Network to join
# - BACKEND_PATH, FRONTEND_PATH: Source code paths (absolute)
# - DOCKERFILE_BACKEND, DOCKERFILE_FRONTEND: Dockerfile paths (absolute)
# - DB_DUMP: Database dump file (relative to dumps/)
# - Plus all app-specific vars (POSTGRES_*, etc.)

services:
  db:
    image: postgis/postgis:15-3.4
    container_name: ${DEPLOYMENT_NAME}_db
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data
      # Database dump loaded on init (if volume is empty)
      - ./dumps/${DB_DUMP}:/docker-entrypoint-initdb.d/dump.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - default

  redis:
    image: redis:7-alpine
    container_name: ${DEPLOYMENT_NAME}_redis
    volumes:
      - redisdata:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - default

  backend:
    build:
      context: ${BACKEND_PATH}
      dockerfile: ${DOCKERFILE_BACKEND}
      target: runtime
    container_name: ${DEPLOYMENT_NAME}_backend
    environment:
      - SECRET_KEY
      - DEBUG
      - ALLOWED_HOSTS
      - DJANGO_ENV
      - DB_NAME=${POSTGRES_DB}
      - DB_USER=${POSTGRES_USER}
      - DB_PASSWORD=${POSTGRES_PASSWORD}
      - DB_HOST=db
      - DB_PORT=5432
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
      - CORS_ALLOW_ALL
      - CORS_ALLOWED_ORIGINS
      # Uvicorn config (dev: 1 worker + reload, prod: 4 workers)
      - WORKERS=${BACKEND_WORKERS:-1}
      - RELOAD=${BACKEND_RELOAD:---reload}
      # Google
      - SUBJECT_CALENDAR
      - SHEET_ID
      - RANGE_NAME
      - GOOGLE_MAPS_API_KEY
      # Analytics
      - GA4_MEASUREMENT_ID
      - AMPLITUDE_API_KEY
      - HOTJAR_API_KEY
      # Payments
      - ACCESS_TOKEN_MERCADO_PAGO
      - MP_PLATFORM_ACCESS_TOKEN
      - USER_ID
      # Push
      - WEBPUSH_VAPID_PUBLIC_KEY
      - WEBPUSH_VAPID_PRIVATE_KEY
      - WEBPUSH_VAPID_ADMIN_EMAIL
      # Init
      - USER_PASSWORD
    volumes:
      - ${BACKEND_PATH}:/app
      - backend_static:/var/etc/static
      - backend_media:/app/media
    ports:
      - "${BACKEND_PORT}:8000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - default
    command: >
      sh -c "python manage.py migrate --noinput &&
             uvicorn amar_django_back.asgi:application --host 0.0.0.0 --port 8000 --workers $${WORKERS:-1} $${RELOAD}"

  celery:
    build:
      context: ${BACKEND_PATH}
      dockerfile: ${DOCKERFILE_BACKEND}
      target: runtime
    container_name: ${DEPLOYMENT_NAME}_celery
    environment:
      - SECRET_KEY
      - DEBUG
      - DJANGO_ENV
      - DB_NAME=${POSTGRES_DB}
      - DB_USER=${POSTGRES_USER}
      - DB_PASSWORD=${POSTGRES_PASSWORD}
      - DB_HOST=db
      - DB_PORT=5432
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    volumes:
      - ${BACKEND_PATH}:/app
      - backend_media:/app/media
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - default
    command: celery -A amar_django_back worker -l INFO

  celery-beat:
    build:
      context: ${BACKEND_PATH}
      dockerfile: ${DOCKERFILE_BACKEND}
      target: runtime
    container_name: ${DEPLOYMENT_NAME}_celery_beat
    environment:
      - SECRET_KEY
      - DEBUG
      - DJANGO_ENV
      - DB_NAME=${POSTGRES_DB}
      - DB_USER=${POSTGRES_USER}
      - DB_PASSWORD=${POSTGRES_PASSWORD}
      - DB_HOST=db
      - DB_PORT=5432
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    volumes:
      - ${BACKEND_PATH}:/app
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - default
    command: celery -A amar_django_back beat -l INFO

  frontend:
    build:
      context: ${FRONTEND_PATH}
      dockerfile: ${DOCKERFILE_FRONTEND}
      target: development
    container_name: ${DEPLOYMENT_NAME}_frontend
    environment:
      - NEXT_PUBLIC_APP_API_URL_BACKOFFICE
      - NEXT_PUBLIC_APP_API_URL_STAGE
      - NEXT_PUBLIC_IS_STAGE
      - NEXT_PUBLIC_FB_PIXEL_ID
      - NEXT_PUBLIC_TAG_MANAGER
      - NEXT_PUBLIC_WHATSAPP_CONTACT
      - NEXT_PUBLIC_API_KEY
      - NEXT_PUBLIC_AMPLITUDE_API_KEY
      - NEXT_PUBLIC_GMAPS_API_KEY
    volumes:
      - ${FRONTEND_PATH}/src:/app/src
      - ${FRONTEND_PATH}/public:/app/public
    ports:
      - "${FRONTEND_PORT}:3000"
    depends_on:
      - backend
    networks:
      - default
    # CMD from Dockerfile.frontend development stage: npm run dev

volumes:
  pgdata:
  redisdata:
  backend_static:
  backend_media:

networks:
  default:
    external: true
    name: ${NETWORK_NAME}
