-- Enable UUID extension if not enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- 1. Site Contents Table (Genel İçerikler)
CREATE TABLE IF NOT EXISTS public.site_contents (
    key TEXT PRIMARY KEY,
    value TEXT,
    type TEXT DEFAULT 'text', -- 'text', 'image_url', 'html', 'json'
    section TEXT, -- 'home', 'about', 'contact', 'footer'
    created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);

-- RLS: Public read, Admin write
ALTER TABLE public.site_contents ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Allow public read access on site_contents" ON public.site_contents;
CREATE POLICY "Allow public read access on site_contents"
ON public.site_contents FOR SELECT TO anon, authenticated
USING (true);

DROP POLICY IF EXISTS "Allow authenticated update on site_contents" ON public.site_contents;
CREATE POLICY "Allow authenticated update on site_contents"
ON public.site_contents FOR UPDATE TO authenticated
USING (true)
WITH CHECK (true);

DROP POLICY IF EXISTS "Allow authenticated insert on site_contents" ON public.site_contents;
CREATE POLICY "Allow authenticated insert on site_contents"
ON public.site_contents FOR INSERT TO authenticated
WITH CHECK (true);

-- 2. Services Table (Hizmetler)
CREATE TABLE IF NOT EXISTS public.services (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    image_url TEXT,
    "order" INTEGER DEFAULT 0,
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);

ALTER TABLE public.services ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Allow public read on services" ON public.services;
CREATE POLICY "Allow public read on services"
ON public.services FOR SELECT TO anon, authenticated
USING (is_active = true OR auth.role() = 'authenticated');

DROP POLICY IF EXISTS "Allow admin all on services" ON public.services;
CREATE POLICY "Allow admin all on services"
ON public.services FOR ALL TO authenticated
USING (true)
WITH CHECK (true);

-- 3. Gallery Table (Galeri)
CREATE TABLE IF NOT EXISTS public.gallery (
    id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
    image_url TEXT NOT NULL,
    caption TEXT,
    category TEXT DEFAULT 'Genel',
    "order" INTEGER DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL
);

ALTER TABLE public.gallery ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Allow public read on gallery" ON public.gallery;
CREATE POLICY "Allow public read on gallery"
ON public.gallery FOR SELECT TO anon, authenticated
USING (true);

DROP POLICY IF EXISTS "Allow admin all on gallery" ON public.gallery;
CREATE POLICY "Allow admin all on gallery"
ON public.gallery FOR ALL TO authenticated
USING (true)
WITH CHECK (true);


-- 4. Storage Bucket for Public Site
INSERT INTO storage.buckets (id, name, public)
VALUES ('public-site', 'public-site', true)
ON CONFLICT (id) DO NOTHING;

-- Storage Policies (Renamed to avoid conflicts)
-- Note: 'storage.objects' is a shared table, so policy names must be unique or scoped.

-- Allow public read SPECIFIC to this bucket
DROP POLICY IF EXISTS "Public Access public-site" ON storage.objects;
CREATE POLICY "Public Access public-site"
ON storage.objects FOR SELECT
TO public
USING ( bucket_id = 'public-site' );

-- Allow authenticated upload/delete SPECIFIC to this bucket
DROP POLICY IF EXISTS "Authenticated Insert public-site" ON storage.objects;
CREATE POLICY "Authenticated Insert public-site"
ON storage.objects FOR INSERT
TO authenticated
WITH CHECK ( bucket_id = 'public-site' );

DROP POLICY IF EXISTS "Authenticated Update public-site" ON storage.objects;
CREATE POLICY "Authenticated Update public-site"
ON storage.objects FOR UPDATE
TO authenticated
USING ( bucket_id = 'public-site' );

DROP POLICY IF EXISTS "Authenticated Delete public-site" ON storage.objects;
CREATE POLICY "Authenticated Delete public-site"
ON storage.objects FOR DELETE
TO authenticated
USING ( bucket_id = 'public-site' );

-- Seed Data (Initial Content)
INSERT INTO public.site_contents (key, value, type, section) VALUES
('site_title', 'Rüya Düğün Salonu', 'text', 'general'),
('hero_title', 'Hayallerinizdeki Düğün İçin', 'text', 'home'),
('hero_subtitle', 'Unutulmaz anlar, profesyonel hizmet ve şık atmosfer.', 'text', 'home'),
('contact_phone', '+90 555 123 45 67', 'text', 'contact'),
('contact_address', 'Atatürk Mah. Karanfil Sok. No:5, İstanbul', 'text', 'contact'),
('site_logo', '', 'image_url', 'general')
ON CONFLICT (key) DO NOTHING;
