-- 09-site-settings.sql
-- Site-wide settings (key-value). Used for configurable assets like hero image.

create table if not exists public.site_settings (
    key         text primary key,
    value       text not null default '',
    updated_at  timestamptz not null default now()
);

alter table public.site_settings enable row level security;

-- Anyone can read settings (public site needs hero image URL)
drop policy if exists "settings_public_read" on public.site_settings;
create policy "settings_public_read"
    on public.site_settings for select
    using (true);

-- Only authenticated admins can modify
drop policy if exists "settings_admin_all" on public.site_settings;
create policy "settings_admin_all"
    on public.site_settings for all to authenticated
    using (true) with check (true);

grant select on public.site_settings to anon, authenticated, service_role;
grant all on public.site_settings to authenticated, service_role;

-- Seed default hero image
insert into public.site_settings (key, value)
values ('hero_image_url', '/images/ferrari-main-car.png')
on conflict (key) do nothing;

notify pgrst, 'reload schema';
