version: "3.8"

services:
  treasure-api:
    image: registry.gitlab.com/treasure-hunt4/treasure-api:latest
    restart: unless-stopped
    ports:
      - "8000:80" # API dispo sur http://IP_DU_SERVEUR:8000
    environment:
      APP_ENV: "production"
      APP_KEY: "${APP_KEY}"
      APP_URL: "${APP_URL}"
      APP_DEBUG: "false"
      SCOUT_DRIVER: "${SCOUT_DRIVER}"
      MEILISEARCH_HOST: "${MEILISEARCH_HOST}"
      MEILISEARCH_KEY: "${MEILISEARCH_KEY}"
      DB_CONNECTION: "mysql"
      DB_HOST: "mysql"
      DB_PORT: 3306
      DB_DATABASE: "${DB_DATABASE}"
      DB_USERNAME: "${DB_USERNAME}"
      DB_PASSWORD: "${DB_PASSWORD}"

  # treasure-api-migrate:
  #   image: registry.gitlab.com/treasure-hunt4/treasure-api:latest
  #   restart: "no"
  #   depends_on:
  #     - mysql
  #   environment:
  #     APP_ENV: "production"
  #     APP_KEY: "${APP_KEY}"
  #     APP_URL: "${APP_URL}"
  #     SCOUT_DRIVER: "${SCOUT_DRIVER}"
  #     MEILISEARCH_HOST: "${MEILISEARCH_HOST}"
  #     MEILISEARCH_KEY: "${MEILISEARCH_KEY}"

  #     DB_CONNECTION: "mysql"
  #     DB_HOST: "mysql"
  #     DB_PORT: 3306
  #     DB_DATABASE: "${DB_DATABASE}"
  #     DB_USERNAME: "${DB_USERNAME}"
  #     DB_PASSWORD: "${DB_PASSWORD}"

  #   # On attend que MySQL soit prêt et on retente la migration jusqu'à succès
  #   command: >
  #     sh -c "
  #       echo '⏳ Waiting for MySQL on mysql:3306...';
  #       php -r '
  #         $host = getenv(\"DB_HOST\") ?: \"mysql\";
  #         $port = getenv(\"DB_PORT\") ?: \"3306\";
  #         $db = getenv(\"DB_DATABASE\") ?: \"\";
  #         $user = getenv(\"DB_USERNAME\") ?: \"\";
  #         $pass = getenv(\"DB_PASSWORD\") ?: \"\";
  #         while (true) {
  #           try {
  #             new PDO(\"mysql:host={$host};port={$port};dbname={$db}\", $user, $pass);
  #             break;
  #           } catch (Throwable $e) {
  #             echo \"MySQL not ready: {$e->getMessage()}\\n\";
  #             sleep(2);
  #           }
  #         }
  #       ';
  #       echo '✅ MySQL is up, running migrations...';
  #       until php artisan migrate --force --no-interaction; do
  #         echo '❌ Migration failed, retrying in 5s...';
  #         sleep 5;
  #       done;
  #       echo '✅ Migrations applied.';
  #       echo '🔗 Creating storage symlink...';
  #       php artisan storage:link || true;
  #       echo '✅ Storage link done.';
  #     "

  phpmyadmin:
    image: phpmyadmin:latest
    restart: unless-stopped
    depends_on:
      - mysql
    ports:
      - "8887:80"
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
      PMA_USER: "${DB_USERNAME}"
      PMA_PASSWORD: "${DB_PASSWORD}"

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      MYSQL_DATABASE: "${DB_DATABASE}"
      MYSQL_USER: "${DB_USERNAME}"
      MYSQL_PASSWORD: "${DB_PASSWORD}"
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - "3308:3306" # Optionnel, pour accès externe à MySQL
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-p${MYSQL_ROOT_PASSWORD}" ]
      interval: 5s
      timeout: 5s
      retries: 10
      start_period: 10s
  meilisearch:
    image: getmeili/meilisearch:v1.7
    restart: unless-stopped
    environment:
      MEILI_MASTER_KEY: "${MEILISEARCH_KEY}"
      MEILI_ENV: "production"
    volumes:
      - meilisearch-data:/meili_data
    ports:
      - "7700:7700"

volumes:
  mysql-data:
  meilisearch-data:
