services:
  db:
    image: mysql:8.0@sha256:d0304ed9fdb64a3f6c7ad11a5fb4f13abfc10e6dfa3f288d652e7320c34df7f9
    container_name: procuratio-db-prod
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE:?MYSQL_DATABASE is required}
      MYSQL_USER: ${MYSQL_USER:?MYSQL_USER is required}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:?MYSQL_PASSWORD is required}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?MYSQL_ROOT_PASSWORD is required}
    volumes:
      - db_data_prod:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u$${MYSQL_USER} -p$${MYSQL_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 20

  api:
    build:
      context: ../api
      dockerfile: Dockerfile.prod
    container_name: procuratio-api-prod
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      APP_ENV: prod
      APP_SECRET: ${APP_SECRET:?APP_SECRET is required}
      MYSQL_DATABASE: ${MYSQL_DATABASE:?MYSQL_DATABASE is required}
      MYSQL_USER: ${MYSQL_USER:?MYSQL_USER is required}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:?MYSQL_PASSWORD is required}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?MYSQL_ROOT_PASSWORD is required}
      DATABASE_URL: ${DATABASE_URL:?DATABASE_URL is required}
      JWT_PASSPHRASE: ${JWT_PASSPHRASE:?JWT_PASSPHRASE is required}
      CORS_ALLOW_ORIGIN: ${CORS_ALLOW_ORIGIN:?CORS_ALLOW_ORIGIN is required}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY:?STRIPE_SECRET_KEY is required}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET:?STRIPE_WEBHOOK_SECRET is required}
      STRIPE_MOCK_MODE: ${STRIPE_MOCK_MODE:-0}
      NOTIFICATIONS_EMAIL_MODE: ${NOTIFICATIONS_EMAIL_MODE:-mail}
      NOTIFICATIONS_SMS_MODE: ${NOTIFICATIONS_SMS_MODE:-webhook}
      NOTIFICATIONS_MAIL_FROM: ${NOTIFICATIONS_MAIL_FROM:?NOTIFICATIONS_MAIL_FROM is required}
      NOTIFICATIONS_MAIL_REPLY_TO: ${NOTIFICATIONS_MAIL_REPLY_TO:-}
      BREVO_API_KEY: ${BREVO_API_KEY:-}
      SMS_WEBHOOK_URL: ${SMS_WEBHOOK_URL:-}
      SMS_WEBHOOK_TOKEN: ${SMS_WEBHOOK_TOKEN:-}
    ports:
      - "${PROD_API_PORT:-48280}:8080"
    healthcheck:
      test: ["CMD-SHELL", "php -r \"exit(@file_get_contents('http://127.0.0.1:8080/api/v1/health')===false?1:0);\""]
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 20s

  stripe-listener:
    image: stripe/stripe-cli:latest@sha256:3daa717082efbbff67e05a122462ca01c6db4bfe91dcfe1105f35794fea0fdc2
    container_name: procuratio-stripe-listener-prod
    restart: unless-stopped
    command:
      - listen
      - --api-key
      - ${STRIPE_SECRET_KEY:?STRIPE_SECRET_KEY is required}
      - --forward-to
      - http://api:8080/api/v1/payments/stripe/webhook
    depends_on:
      api:
        condition: service_healthy

  frontend:
    build:
      context: ../frontend
      dockerfile: Dockerfile.prod
      args:
        VITE_API_BASE_URL: ${VITE_API_BASE_URL:?VITE_API_BASE_URL is required}
        VITE_STRIPE_PUBLIC_KEY: ${VITE_STRIPE_PUBLIC_KEY:-}
    container_name: procuratio-frontend-prod
    restart: unless-stopped
    depends_on:
      api:
        condition: service_healthy
    ports:
      - "${PROD_WEB_PORT:-48200}:80"
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1/health >/dev/null || exit 1"]
      interval: 20s
      timeout: 5s
      retries: 10
      start_period: 30s

  adminer:
    image: adminer:4@sha256:aedd36aebf9352b435a2657f0a1e51fc6ac054dd838f42aee2a79ce059a1d796
    container_name: procuratio-adminer-prod
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "${PROD_ADMINER_PORT:-48281}:8080"

volumes:
  db_data_prod:
