-- Materialized view for MOTIE org events to speed up API access
drop materialized view if exists public.motie_org_events_mv cascade;

create materialized view public.motie_org_events_mv as
select *
from public.motie_org_events;

-- Indexes tuned for typical filters (기간, 이벤트 종류, 이름/부서/직위)
create index if not exists motie_org_events_mv_valid_from_idx
    on public.motie_org_events_mv (valid_from desc);

create index if not exists motie_org_events_mv_event_type_idx
    on public.motie_org_events_mv (event_type, valid_from desc);

create index if not exists motie_org_events_mv_name_idx
    on public.motie_org_events_mv (lower(name));

create index if not exists motie_org_events_mv_department_idx
    on public.motie_org_events_mv (lower(department));

create index if not exists motie_org_events_mv_position_idx
    on public.motie_org_events_mv (lower("position"));

