-- Expose the most-recently-active user's `lang` preference (stored in
-- auth.users.raw_user_meta_data.lang by the dashboard's I18nProvider)
-- as a public view so the daemon can read it via PostgREST. The
-- `auth` schema is restricted from REST access by default.
--
-- Single-user assumption: returns one row with the latest user's lang.
-- Multi-user setups should replace this with a per-(client_id) mapping.
--
-- Apply once after 0001..0008.

create or replace view public.user_locale_pref as
select raw_user_meta_data->>'lang' as lang
from auth.users
where raw_user_meta_data->>'lang' is not null
order by last_sign_in_at desc nulls last
limit 1;

-- service_role bypasses RLS but the view inherits no RLS. Be explicit:
revoke all on public.user_locale_pref from anon, authenticated;
grant select on public.user_locale_pref to service_role;
