-- 2026-04-30: 통합 이미지 bucket 'site-images' (제품/공지/QnA/후기 모두)
-- 적용: Supabase Studio → SQL Editor에서 1회 실행

-- 1. bucket 생성 (이미 있으면 무시)
insert into storage.buckets (id, name, public)
values ('site-images', 'site-images', true)
on conflict (id) do nothing;

-- 2. RLS — public read (anon이 업로드한 이미지 누구나 볼 수 있음)
do $$
begin
  if not exists (
    select 1 from pg_policies
    where schemaname = 'storage' and tablename = 'objects' and policyname = 'site_images_public_read'
  ) then
    create policy "site_images_public_read"
      on storage.objects for select
      using (bucket_id = 'site-images');
  end if;
end$$;

-- 3. RLS — public insert (anon이 후기 이미지 업로드 가능 + admin이 콘텐츠 업로드)
do $$
begin
  if not exists (
    select 1 from pg_policies
    where schemaname = 'storage' and tablename = 'objects' and policyname = 'site_images_public_insert'
  ) then
    create policy "site_images_public_insert"
      on storage.objects for insert
      with check (bucket_id = 'site-images');
  end if;
end$$;
