create table if not exists llm_provider_configs (
  id uuid primary key default gen_random_uuid(),
  provider_key text not null unique,
  api_key text not null,
  base_url text not null,
  is_active boolean not null default true,
  configured_by text,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  constraint llm_provider_configs_provider_key_check
    check (provider_key ~ '^[a-z0-9_-]+$')
);

create table if not exists llm_stage_routes (
  stage text primary key,
  provider_key text not null references llm_provider_configs(provider_key) on update cascade,
  model text not null,
  updated_by text,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  constraint llm_stage_routes_stage_check
    check (stage in (
      'document_ocr',
      'document_extraction',
      'standardization',
      'keyword_semantic',
      'search_vision',
      'fixed_search_batch',
      'chatbot_intent'
    ))
);

create index if not exists idx_llm_provider_configs_active
  on llm_provider_configs(is_active, provider_key);

create index if not exists idx_llm_stage_routes_provider
  on llm_stage_routes(provider_key);
