services:
  # Supabase PostgreSQL (includes auth schema + extensions)
  db:
    image: supabase/postgres:15.8.1.060
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres}
      POSTGRES_DB: postgres
    volumes:
      - calctext-db:/var/lib/postgresql/data
      - ./supabase/migrations:/docker-entrypoint-initdb.d/migrations
    healthcheck:
      test: pg_isready -U postgres -d postgres
      interval: 5s
      timeout: 5s
      retries: 10

  # Supabase Auth (GoTrue)
  auth:
    image: supabase/gotrue:v2.158.1
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      GOTRUE_API_HOST: 0.0.0.0
      GOTRUE_API_PORT: 9999
      API_EXTERNAL_URL: ${SITE_URL:-http://localhost:5173}/auth
      GOTRUE_DB_DRIVER: postgres
      GOTRUE_DB_DATABASE_URL: postgres://supabase_auth_admin:${DB_PASSWORD:-postgres}@db:5432/postgres?sslmode=disable&search_path=auth
      GOTRUE_SITE_URL: ${SITE_URL:-http://localhost:5173}
      GOTRUE_URI_ALLOW_LIST: ${SITE_URL:-http://localhost:5173}/**
      GOTRUE_DISABLE_SIGNUP: 'false'
      GOTRUE_JWT_SECRET: ${JWT_SECRET}
      GOTRUE_JWT_EXP: 3600
      GOTRUE_JWT_DEFAULT_GROUP_NAME: authenticated
      GOTRUE_EXTERNAL_EMAIL_ENABLED: 'true'
      GOTRUE_MAILER_AUTOCONFIRM: 'true'
      GOTRUE_SMTP_ADMIN_EMAIL: admin@calctext.local

  # PostgREST (REST API for database)
  rest:
    image: postgrest/postgrest:v12.2.8
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      PGRST_DB_URI: postgres://postgres:${DB_PASSWORD:-postgres}@db:5432/postgres
      PGRST_DB_SCHEMAS: public
      PGRST_DB_ANON_ROLE: anon
      PGRST_JWT_SECRET: ${JWT_SECRET}
      PGRST_DB_USE_LEGACY_GUCS: 'false'

  # Hocuspocus collaboration server
  collab:
    build: ./collab-server
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      PORT: 4000
      DATABASE_URL: postgres://postgres:${DB_PASSWORD:-postgres}@db:5432/postgres
      JWT_SECRET: ${JWT_SECRET}

  # Web frontend (nginx + static files)
  web:
    build:
      context: .
      args:
        VITE_SUPABASE_URL: ${SITE_URL:-http://localhost:8080}
        VITE_SUPABASE_ANON_KEY: ${ANON_KEY}
        VITE_COLLAB_WS_URL: ${COLLAB_WS_URL:-ws://localhost:8080/ws}
    restart: unless-stopped
    ports:
      - "${PORT:-8080}:8080"
    depends_on:
      - auth
      - rest
      - collab

volumes:
  calctext-db:
