services:
  db:
    image: postgres:18
    restart: unless-stopped
    environment:
      POSTGRES_USER: inventory
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
      POSTGRES_DB: inventory2026
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U inventory -d inventory2026"]
      interval: 5s
      timeout: 5s
      retries: 20

  api:
    build:
      context: .
      dockerfile: apps/api/Dockerfile
    restart: unless-stopped
    environment:
      DATABASE_URL: postgres://inventory:${POSTGRES_PASSWORD}@db:5432/inventory2026
      API_PORT: 4000
      SESSION_SECRET: ${SESSION_SECRET:?set SESSION_SECRET in .env}
      CORS_ORIGIN: ${CORS_ORIGIN:?set CORS_ORIGIN in .env}
      PUBLIC_APP_URL: ${PUBLIC_APP_URL:?set PUBLIC_APP_URL in .env}
      PASSKEY_RP_NAME: ${PASSKEY_RP_NAME:-Inventory2026}
      PASSKEY_ORIGIN: ${PASSKEY_ORIGIN:?set PASSKEY_ORIGIN in .env}
      PASSKEY_RP_ID: ${PASSKEY_RP_ID:?set PASSKEY_RP_ID in .env}
      SMTP_HOST: ${SMTP_HOST:-}
      SMTP_PORT: ${SMTP_PORT:-587}
      SMTP_USER: ${SMTP_USER:-}
      SMTP_PASS: ${SMTP_PASS:-}
      SMTP_FROM: ${SMTP_FROM:-Inventory2026 <inventory@example.com>}
    expose:
      - "4000"
    depends_on:
      db:
        condition: service_healthy

  web:
    build:
      context: .
      dockerfile: apps/web/Dockerfile
    restart: unless-stopped
    environment:
      PUBLIC_API_URL: ${PUBLIC_API_URL:-same-origin}
      NEXT_PUBLIC_API_URL: ${PUBLIC_API_URL:-same-origin}
    expose:
      - "3000"
    depends_on:
      - api

  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    environment:
      DOMAIN: ${DOMAIN:?set DOMAIN in .env}
      ACME_EMAIL: ${ACME_EMAIL:?set ACME_EMAIL in .env}
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./deploy/Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      - web
      - api

volumes:
  postgres-data:
  caddy-data:
  caddy-config:
