drop policy if exists email_event_metadata_overrides_admin_write on public.email_event_metadata_overrides;

create policy email_event_metadata_overrides_write_country_scope
on public.email_event_metadata_overrides
for all
using (
  public.is_admin()
  or exists (
    select 1
    from public.raw_email_events ree
    where ree.sent_email_name = email_event_metadata_overrides.sent_email_name
      and public.has_country_access(ree.primary_country_code)
  )
)
with check (
  public.is_admin()
  or exists (
    select 1
    from public.raw_email_events ree
    where ree.sent_email_name = email_event_metadata_overrides.sent_email_name
      and public.has_country_access(ree.primary_country_code)
  )
);

