create table if not exists public.mindmap_user_configs (
    user_id uuid primary key references public.profiles(id) on delete cascade,
    config jsonb not null default '{}'::jsonb,
    updated_at timestamptz not null default timezone('utc', now())
);

create or replace function public.set_mindmap_user_configs_updated_at()
returns trigger
language plpgsql
security invoker
as $$
begin
    new.updated_at := timezone('utc', now());
    return new;
end;
$$;

drop trigger if exists set_mindmap_user_configs_updated_at on public.mindmap_user_configs;
create trigger set_mindmap_user_configs_updated_at
before update on public.mindmap_user_configs
for each row
execute procedure public.set_mindmap_user_configs_updated_at();

alter table public.mindmap_user_configs enable row level security;

drop policy if exists "Select own mindmap config" on public.mindmap_user_configs;
create policy "Select own mindmap config"
    on public.mindmap_user_configs
    for select
    using (auth.uid() = user_id);

drop policy if exists "Insert own mindmap config" on public.mindmap_user_configs;
create policy "Insert own mindmap config"
    on public.mindmap_user_configs
    for insert
    with check (auth.uid() = user_id);

drop policy if exists "Update own mindmap config" on public.mindmap_user_configs;
create policy "Update own mindmap config"
    on public.mindmap_user_configs
    for update
    using (auth.uid() = user_id)
    with check (auth.uid() = user_id);
