version: "3.8"

services:
  api:
    image: jhumanj/opnform-api:dev
    container_name: opnform-api
    volumes:
      - ./api:/usr/share/nginx/html:delegated
      - /usr/share/nginx/html/vendor # Exclude vendor directory from the mount
      - ./api/storage:/usr/share/nginx/html/storage:delegated
    environment:
      # Core Laravel settings
      APP_ENV: local
      APP_DEBUG: "true"
      APP_DEV_CORS: "true"
      APP_URL: http://localhost
      FRONT_URL: http://localhost:3000
      APP_KEY: base64:dD8AFEB45nKZ0VWmIPiW495rDYb+ywRJnCCSdycw0pI=
      JWT_SECRET: 38hjE96gYu4ceKSSHNnzgeynMr2m3WkNBN2u7zzpVeiITtxFEloxAjcr7TCKuiiy
      JWT_SKIP_IP_UA_VALIDATION: "true"
      # Database settings
      DB_HOST: db
      DB_DATABASE: ${DB_DATABASE:-forge}
      DB_USERNAME: ${DB_USERNAME:-forge}
      DB_PASSWORD: ${DB_PASSWORD:-forge}
      DB_CONNECTION: ${DB_CONNECTION:-pgsql}
      # Override production settings for minimal setup
      CACHE_DRIVER: file
      SESSION_DRIVER: file
      QUEUE_CONNECTION: sync
      # Storage settings
      FILESYSTEM_DRIVER: local
      LOCAL_FILESYSTEM_VISIBILITY: public
      # Mail settings (development - uses log driver)
      MAIL_MAILER: log
      MAIL_FROM_ADDRESS: dev@localhost
      MAIL_FROM_NAME: OpnForm-Dev
      # PHP Configuration
      PHP_MEMORY_LIMIT: "1G"
      PHP_MAX_EXECUTION_TIME: "600"
      PHP_UPLOAD_MAX_FILESIZE: "64M"
      PHP_POST_MAX_SIZE: "64M"
      # Development settings
      PHP_IDE_CONFIG: serverName=Docker
      XDEBUG_MODE: ${XDEBUG_MODE:-off}
      XDEBUG_CONFIG: client_host=host.docker.internal
    depends_on:
      db:
        condition: service_healthy

  ui:
    image: jhumanj/opnform-client:dev
    container_name: opnform-client
    depends_on:
      api:
        condition: service_started
    command: sh -c "
      if [ ! -f node_modules/.install-complete ] || [ package.json -nt node_modules/.install-complete ]; then
      echo 'Installing dependencies...';
      npm install && touch node_modules/.install-complete;
      else
      echo 'Dependencies up to date, skipping install';
      fi && npm run docker-dev"
    volumes:
      - ./client:/app:delegated
      - client_node_modules:/app/node_modules # Named volume for persistence
    environment:
      NODE_ENV: development
      NUXT_PUBLIC_APP_ENV: development
      NUXT_PUBLIC_APP_URL: "http://localhost:3000"
      NUXT_PUBLIC_API_BASE: "http://localhost"
      NUXT_PRIVATE_API_BASE: "http://opnform-ingress"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "3000:3000" # Main dev server
      - "24678:24678" # Vite HMR port

  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: 5s
      timeout: 5s
      retries: 5
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data

  ingress:
    image: nginx:1
    container_name: opnform-ingress
    volumes:
      - ./docker/nginx.dev.conf:/etc/nginx/templates/default.conf.template
    environment:
      NGINX_HOST: localhost
      NGINX_PORT: 80
      NGINX_MAX_BODY_SIZE: 64m
    ports:
      - "80:80"
    depends_on:
      api:
        condition: service_started
      ui:
        condition: service_started

volumes:
  postgres-data:
  client_node_modules:
