-- CRM date correctness: preserve lead birth vs status change timing. -- Run in Supabase SQL editor (service role / dashboard). alter table public.leads add column if not exists updated_at timestamptz; alter table public.leads add column if not exists status_changed_at timestamptz; comment on column public.leads.created_at is 'When the contact entered Clientify / first seen (alta).'; comment on column public.leads.updated_at is 'Last webhook/sync touch.'; comment on column public.leads.status_changed_at is 'When status last changed (e.g. → cliente). Use this for “ventas en el periodo”.'; -- Backfill: unknown status timing → treat as created_at (best effort). update public.leads set updated_at = coalesce(updated_at, created_at, now()), status_changed_at = coalesce(status_changed_at, created_at, now()) where updated_at is null or status_changed_at is null;