services:
  db:
    image: mysql:8.0@sha256:d0304ed9fdb64a3f6c7ad11a5fb4f13abfc10e6dfa3f288d652e7320c34df7f9
    container_name: procuratio-db
    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}
    ports:
      - "43306:3306"
    volumes:
      - db_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h localhost -u$${MYSQL_USER} -p$${MYSQL_PASSWORD}"]
      interval: 5s
      timeout: 5s
      retries: 20

  api:
    build:
      context: ../api
      dockerfile: Dockerfile
    container_name: procuratio-api
    working_dir: /app
    command: sh /app/docker/entrypoint.sh
    volumes:
      - ../api:/app
    ports:
      - "48180:8080"
    depends_on:
      db:
        condition: service_healthy
    environment:
      APP_ENV: ${APP_ENV:-dev}
      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}
      # On lit l'URL complete depuis l'environnement pour eviter de figer des motifs sensibles dans le compose.
      DATABASE_URL: ${DATABASE_URL:?DATABASE_URL is required}
      JWT_PASSPHRASE: ${JWT_PASSPHRASE:?JWT_PASSPHRASE is required}
      CORS_ALLOW_ORIGIN: ${CORS_ALLOW_ORIGIN:-^https?://localhost(:[0-9]+)?$}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY:-sk_test_change_me}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET:-whsec_change_me}
      STRIPE_MOCK_MODE: ${STRIPE_MOCK_MODE:-1}

  stripe-listener:
    image: stripe/stripe-cli:latest@sha256:3daa717082efbbff67e05a122462ca01c6db4bfe91dcfe1105f35794fea0fdc2
    container_name: procuratio-stripe-listener
    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
    restart: unless-stopped

  frontend:
    build:
      context: ../frontend
      dockerfile: Dockerfile
    container_name: procuratio-frontend
    command: sh -c "npm install && npm run dev -- --host 0.0.0.0 --port 3000"
    volumes:
      - ../frontend:/app
      - /app/node_modules
      - frontend_cypress_cache:/root/.cache/Cypress
    ports:
      - "43100:3000"
    depends_on:
      - api
    environment:
      VITE_API_BASE_URL: http://localhost:48180
      CYPRESS_BASE_URL: http://localhost:43100
      VITE_API_BASE_URL_CYPRESS: ""
      VITE_PROXY_TARGET: http://api:8080
      # Active uniquement en execution Cypress; en usage navigateur local,
      # il faut garder l'URL publique localhost pour eviter les "Failed to fetch".
      VITE_E2E_DOCKER: "false"

  adminer:
    image: adminer:4@sha256:aedd36aebf9352b435a2657f0a1e51fc6ac054dd838f42aee2a79ce059a1d796
    container_name: procuratio-adminer
    ports:
      - "48181:8080"
    depends_on:
      - db

volumes:
  db_data:
  frontend_cypress_cache:
