---
services:
  api: &api-environment
    build:
      context: .
      dockerfile: docker/Dockerfile.api
      args:
        APP_VERSION: "local-test-1.0.0"
    container_name: opnform-api
    volumes: &api-environment-volumes
      - opnform_storage:/usr/share/nginx/html/storage:rw
    environment: &api-env
      APP_ENV: production
      # Database settings
      DB_HOST: db
      REDIS_HOST: redis
      DB_DATABASE: ${DB_DATABASE:-forge}
      DB_USERNAME: ${DB_USERNAME:-forge}
      DB_PASSWORD: ${DB_PASSWORD:-forge}
      DB_CONNECTION: ${DB_CONNECTION:-pgsql}
      # PHP Configuration
      PHP_MEMORY_LIMIT: "1G"
      PHP_MAX_EXECUTION_TIME: "600"
      PHP_UPLOAD_MAX_FILESIZE: "64M"
      PHP_POST_MAX_SIZE: "64M"
    env_file:
      - ./api/.env
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy # Depend on redis being healthy too
    healthcheck:
      test: ["CMD-SHELL", "php /usr/share/nginx/html/artisan about || exit 1"]
      interval: 30s
      timeout: 15s
      retries: 3
      start_period: 60s

  api-worker:
    <<: *api-environment
    container_name: opnform-api-worker
    command: ["php", "artisan", "queue:work"]
    environment:
      <<: *api-env
      APP_ENV: production
    healthcheck:
      test: ["CMD-SHELL", "pgrep -f 'php artisan queue:work' > /dev/null || exit 1"]
      interval: 60s
      timeout: 10s
      retries: 3
      start_period: 30s

  api-scheduler:
    <<: *api-environment
    container_name: opnform-api-scheduler
    command: ["php", "artisan", "schedule:work"]
    environment:
      <<: *api-env
      APP_ENV: production
    healthcheck:
      test: ["CMD-SHELL", "php /usr/share/nginx/html/artisan app:scheduler-status --mode=check --max-minutes=3 || exit 1"]
      interval: 60s
      timeout: 30s
      retries: 3
      start_period: 70s # Allow time for first scheduled run and cache write

  ui:
    build:
      context: .
      dockerfile: docker/Dockerfile.client
      args:
        APP_VERSION: "local-test-1.0.0"
    container_name: opnform-client
    env_file:
      - ./client/.env
    healthcheck:
      test: ["CMD-SHELL", "wget --spider -q http://localhost:3000/login || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 45s

  redis:
    image: redis:7
    container_name: opnform-redis
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
      interval: 30s
      timeout: 5s

  db:
    image: postgres:16
    container_name: opnform-db
    environment:
      POSTGRES_DB: ${DB_DATABASE:-forge}
      POSTGRES_USER: ${DB_USERNAME:-forge}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-forge}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-forge}"]
      interval: 30s
      timeout: 5s
    volumes:
      - postgres-data:/var/lib/postgresql/data

  ingress:
    image: nginx:1
    container_name: opnform-ingress
    volumes:
      - ./docker/nginx.conf:/etc/nginx/templates/default.conf.template
    ports:
      - 80:80
    environment:
      - NGINX_MAX_BODY_SIZE=64m
    depends_on:
      api:
        condition: service_started
      ui:
        condition: service_started
    healthcheck:
      test: ["CMD-SHELL", "nginx -t && curl -f http://localhost/ || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

volumes:
  postgres-data:
  opnform_storage: