-- Cleanup duplicate columns on motie_org_hist: prefer valid_from/valid_to, compute is_current

-- 1) Ensure downstream views do not depend on these columns anymore
--    Then safely drop duplicate/legacy columns (use IF EXISTS for idempotency)
alter table if exists public.motie_org_hist
  drop column if exists started_at,
  drop column if exists ended_at,
  drop column if exists is_open;

-- 2) Optional: add covering indexes for frequent filters
create index if not exists idx_motie_org_hist_valid_from on public.motie_org_hist (valid_from);
create index if not exists idx_motie_org_hist_valid_to   on public.motie_org_hist (valid_to);
create index if not exists idx_motie_org_hist_name       on public.motie_org_hist (name);

