-- Create site_contents table for dynamic CMS
CREATE TABLE IF NOT EXISTS site_contents (
    key TEXT PRIMARY KEY,
    value TEXT,
    type TEXT CHECK (type IN ('text', 'image_url', 'html', 'long_text', 'json')),
    section TEXT NOT NULL,
    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
);

-- Enable Row Level Security
ALTER TABLE site_contents ENABLE ROW LEVEL SECURITY;

-- Create policies
-- Allow public read access to all site contents (needed for the public website)
CREATE POLICY "Public read access" ON site_contents
    FOR SELECT TO public USING (true);

-- Allow authenticated users (admins) to update content
CREATE POLICY "Admin update access" ON site_contents
    FOR UPDATE TO authenticated USING (true);

-- Allow authenticated users to insert (for initial setup)
CREATE POLICY "Admin insert access" ON site_contents
    FOR INSERT TO authenticated WITH CHECK (true);

-- Insert default contents if they don't exist
INSERT INTO site_contents (key, value, type, section) VALUES
    ('site_title', 'ParaKasa', 'text', 'general'),
    ('site_description', 'ParaKasa Yönetim Paneli', 'long_text', 'general'),
    ('site_logo', '', 'image_url', 'general'),
    ('contact_phone', '', 'text', 'contact'),
    ('contact_email', '', 'text', 'contact'),
    ('contact_address', '', 'long_text', 'contact'),
    ('social_instagram', '', 'text', 'contact'),
    ('social_youtube', '', 'text', 'contact'),
    ('social_tiktok', '', 'text', 'contact'),
    ('contact_map_embed', '', 'html', 'contact')
ON CONFLICT (key) DO NOTHING;
