create type public.app_role as enum ('admin','user');
create table public.user_roles (
  id uuid primary key default gen_random_uuid(),
  user_id uuid references auth.users(id) on delete cascade not null,
  role app_role not null,
  unique (user_id, role)
);
grant select on public.user_roles to authenticated;
grant all on public.user_roles to service_role;
alter table public.user_roles enable row level security;
create policy "Users see own roles" on public.user_roles for select to authenticated using (user_id = auth.uid());

create or replace function public.has_role(_user_id uuid, _role app_role)
returns boolean language sql stable security definer set search_path = public as $$
  select exists (select 1 from public.user_roles where user_id = _user_id and role = _role)
$$;

create or replace function public.handle_first_admin()
returns trigger language plpgsql security definer set search_path = public as $$
begin
  if not exists (select 1 from public.user_roles where role = 'admin') then
    insert into public.user_roles (user_id, role) values (new.id, 'admin');
  end if;
  return new;
end $$;
create trigger on_auth_user_created_admin after insert on auth.users
for each row execute function public.handle_first_admin();

create table public.posts (
  id uuid primary key default gen_random_uuid(),
  title text not null,
  slug text not null unique,
  excerpt text not null default '',
  content text not null default '',
  cover_image_url text,
  published boolean not null default false,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);
grant select on public.posts to anon;
grant select, insert, update, delete on public.posts to authenticated;
grant all on public.posts to service_role;
alter table public.posts enable row level security;
create policy "Public reads published" on public.posts for select to anon, authenticated using (published = true);
create policy "Admins read all" on public.posts for select to authenticated using (public.has_role(auth.uid(),'admin'));
create policy "Admins insert" on public.posts for insert to authenticated with check (public.has_role(auth.uid(),'admin'));
create policy "Admins update" on public.posts for update to authenticated using (public.has_role(auth.uid(),'admin'));
create policy "Admins delete" on public.posts for delete to authenticated using (public.has_role(auth.uid(),'admin'));

insert into public.posts (title, slug, excerpt, content, published) values
('Why Trained Security Guards Matter for Your Business','why-trained-security-guards-matter','Professional, trained guards prevent incidents before they happen. Here is what to look for when hiring.','Hiring trained security personnel protects people, property and reputation.\n\nOur guards go through regular drills, discipline training and customer-handling sessions so they stay alert and courteous on duty.\n\nWhen choosing a manpower partner, check their training process, supervision and response time.',true),
('Event Security & Bouncers: Planning a Safe Celebration','event-security-bouncers-planning','From weddings to corporate events, the right crowd management team keeps every guest safe.','Large gatherings need planned entry control, crowd management and quick response.\n\nOur bouncer team is experienced in weddings, concerts and corporate functions across Hyderabad.',true);