-- Server-side project aggregation. Replaces the dashboard's
-- "fetch 2000 rows + groupBy in JS" pattern, which silently drops
-- long-tail projects once event volume grows.
--
-- security_invoker = true → view executes with the caller's
-- privileges, so the existing "authenticated read all" RLS policy
-- on worker_events naturally applies.
--
-- Apply once after 0001..0011. Idempotent.

create or replace view public.project_summary
with (security_invoker = true) as
select
    project,
    count(*)::bigint as event_count,
    max(created_at) as last_event_at
from public.worker_events
group by project;

grant select on public.project_summary to authenticated, service_role;
