## Kuwait Closed-Loop Prepaid Card — Docker Compose
## Starts: MySQL, the umbrella app (wallet_web), and a one-shot migration runner.

services:
  # ─── Database ─────────────────────────────────────────────────────────────
  db:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD:-dataaegis123}
      MYSQL_DATABASE:      ${DB_NAME:-wallet_app_dev}
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "-u", "root", "-p${DB_PASSWORD:-dataaegis123}"]
      interval: 10s
      timeout: 5s
      retries: 10

  # ─── DB migrations (runs once, exits) ─────────────────────────────────────
  migrate:
    build:
      context: .
      dockerfile: Dockerfile
    command: ["mix", "ecto.migrate"]
    environment:
      DATABASE_URL: "mysql://root:${DB_PASSWORD:-dataaegis123}@db:3306/${DB_NAME:-wallet_app_dev}"
      SECRET_KEY_BASE: ${SECRET_KEY_BASE:-please_set_a_real_secret_key_base_in_production}
      MIX_ENV: prod
    depends_on:
      db:
        condition: service_healthy
    restart: "no"

  # ─── Application ──────────────────────────────────────────────────────────
  app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - "${APP_PORT:-4000}:4000"
    environment:
      DATABASE_URL:     "mysql://root:${DB_PASSWORD:-dataaegis123}@db:3306/${DB_NAME:-wallet_app}"
      SECRET_KEY_BASE:  ${SECRET_KEY_BASE:-please_set_a_real_secret_key_base_in_production}
      PHX_HOST:         ${PHX_HOST:-localhost}
      PORT:             4000
      MIX_ENV:          prod
      # Prepaid-specific
      PREPAID_ENABLED:  ${PREPAID_ENABLED:-true}
    depends_on:
      migrate:
        condition: service_completed_successfully
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:4000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  mysql_data:
    driver: local
