services:
  mysql:
    image: mysql:8.4
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE:-sayfet}
      MYSQL_USER: ${DB_USERNAME:-sayfet}
      MYSQL_PASSWORD: ${DB_PASSWORD:-sayfet}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootroot}
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - "${FORWARD_DB_PORT:-3307}:3306"
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER -p$$MYSQL_PASSWORD"]
      interval: 5s
      timeout: 5s
      retries: 20

  minio:
    image: minio/minio:latest
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${AWS_ACCESS_KEY_ID:-sayfet}
      MINIO_ROOT_PASSWORD: ${AWS_SECRET_ACCESS_KEY:-sayfetsayfet}
    volumes:
      - minio_data:/data
    ports:
      - "${MINIO_PORT:-9000}:9000"
      - "${MINIO_CONSOLE_PORT:-9001}:9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio-init:
    image: minio/mc:latest
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 ${AWS_ACCESS_KEY_ID:-sayfet} ${AWS_SECRET_ACCESS_KEY:-sayfetsayfet};
      mc mb --ignore-existing local/${AWS_BUCKET:-sayfet-uploads};
      mc anonymous set none local/${AWS_BUCKET:-sayfet-uploads};
      "

  mailpit:
    image: axllent/mailpit:latest
    restart: unless-stopped
    environment:
      MP_MAX_MESSAGES: 5000
      MP_SMTP_AUTH_ACCEPT_ANY: 1
      MP_SMTP_AUTH_ALLOW_INSECURE: 1
    ports:
      - "${MAIL_PORT:-1025}:1025"
      - "${MAILPIT_UI_PORT:-8025}:8025"

  app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    depends_on:
      mysql:
        condition: service_healthy
      minio:
        condition: service_healthy
    environment:
      APP_NAME: Sayfet
      APP_ENV: local
      APP_DEBUG: "true"
      APP_URL: http://localhost:8000
      APP_LOCALE: fr
      APP_FALLBACK_LOCALE: en

      DB_CONNECTION: mysql
      DB_HOST: mysql
      DB_PORT: 3306
      DB_DATABASE: ${DB_DATABASE:-sayfet}
      DB_USERNAME: ${DB_USERNAME:-sayfet}
      DB_PASSWORD: ${DB_PASSWORD:-sayfet}

      SESSION_DRIVER: database
      QUEUE_CONNECTION: database
      CACHE_STORE: database
      FILESYSTEM_DISK: s3

      MAIL_MAILER: smtp
      MAIL_HOST: mailpit
      MAIL_PORT: 1025
      MAIL_FROM_ADDRESS: "no-reply@sayfet.local"
      MAIL_FROM_NAME: Sayfet

      AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID:-sayfet}
      AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY:-sayfetsayfet}
      AWS_DEFAULT_REGION: us-east-1
      AWS_BUCKET: ${AWS_BUCKET:-sayfet-uploads}
      AWS_ENDPOINT: http://minio:9000
      AWS_USE_PATH_STYLE_ENDPOINT: "true"

      TWILIO_ACCOUNT_SID: ${TWILIO_ACCOUNT_SID:-}
      TWILIO_AUTH_TOKEN: ${TWILIO_AUTH_TOKEN:-}
      TWILIO_VERIFY_SERVICE_SID: ${TWILIO_VERIFY_SERVICE_SID:-}
      OTP_DEV_FALLBACK: ${OTP_DEV_FALLBACK:-true}
    volumes:
      - ./:/app
    ports:
      - "${APP_PORT:-8000}:8000"
    command: >
      sh -c "
      composer install --no-interaction --prefer-dist &&
      php artisan key:generate --force &&
      php artisan migrate --force &&
      php artisan db:seed --force &&
      php artisan serve --host=0.0.0.0 --port=8000
      "

volumes:
  mysql_data:
  minio_data:
