-- Lead-Inbox: card_leads zum CRM-Lite ausbauen.
-- ====================================================================
-- Bisher waren Leads reine Eingangs-Eintraege ohne Bearbeitungsstatus.
-- Damit MA ihre eingehenden Leads strukturiert abarbeiten koennen
-- (Messen, Lead-Capture-Formulare, gescannte Visitenkarten):
--
--   - lead_status: workflow-state (new/contacted/in_progress/...)
--   - notes: freier Notiztext (z.B. "trifft mich naechste Woche an")
--   - tags: Kategorien (z.B. "messe-2026", "key-account")
--   - follow_up_at: Wiedervorlage-Zeitpunkt
--   - source_kind: woher kommt der Lead (form/reception/scan/manual)
--     ergaenzt die bestehende source_url

alter table public.card_leads
  add column if not exists lead_status text not null default 'new'
    check (lead_status in ('new', 'contacted', 'in_progress', 'converted', 'dismissed')),
  add column if not exists notes text,
  add column if not exists tags text[] not null default '{}',
  add column if not exists follow_up_at timestamptz,
  add column if not exists source_kind text not null default 'form'
    check (source_kind in ('form', 'reception', 'scan', 'manual'));

-- Index fuer "meine offenen Leads" + "Wiedervorlagen heute".
create index if not exists card_leads_employee_status_idx
  on public.card_leads (employee_id, lead_status, created_at desc);
create index if not exists card_leads_follow_up_idx
  on public.card_leads (follow_up_at)
  where follow_up_at is not null and lead_status not in ('converted', 'dismissed');
