import { createClient } from "@/lib/supabase-server"
import { ContentForm } from "@/components/dashboard/content-form"
import { SiteContent } from "@/types/cms"

export default async function ContentPage() {
    const supabase = await createClient()

    const { data: contents } = await supabase
        .from('site_contents')
        .select('*')
        .order('key')

    // Define default contents that should exist
    const DEFAULT_CONTENTS: SiteContent[] = [
        // General
        { key: 'site_title', value: 'ParaKasa', type: 'text', section: 'general' },
        { key: 'site_description', value: '', type: 'long_text', section: 'general' },
        { key: 'site_logo', value: '', type: 'image_url', section: 'general' },

        // Contact
        { key: 'contact_phone', value: '', type: 'text', section: 'contact' },
        { key: 'contact_email', value: '', type: 'text', section: 'contact' },
        { key: 'contact_address', value: '', type: 'long_text', section: 'contact' },
        { key: 'social_instagram', value: '', type: 'text', section: 'contact' },
        { key: 'social_youtube', value: '', type: 'text', section: 'contact' },
        { key: 'social_tiktok', value: '', type: 'text', section: 'contact' },
        { key: 'contact_map_embed', value: '', type: 'html', section: 'contact' },
    ]

    // Merge default contents with existing contents
    const mergedContents = [...(contents as SiteContent[] || [])]
    const existingKeys = new Set(mergedContents.map(c => c.key))

    DEFAULT_CONTENTS.forEach(item => {
        if (!existingKeys.has(item.key)) {
            mergedContents.push(item)
        }
    })

    return (
        <div className="flex-1 space-y-4 p-8 pt-6">
            <div className="flex items-center justify-between space-y-2">
                <h2 className="text-3xl font-bold tracking-tight">İçerik Yönetimi</h2>
            </div>
            <div className="hidden h-full flex-1 flex-col space-y-8 md:flex">
                <div className="grid gap-4 md:grid-cols-1 lg:grid-cols-1">
                    <div className="col-span-1">
                        <div className="space-y-6">
                            <div>
                                <p className="text-sm text-muted-foreground">
                                    Site başlığı, sloganlar, iletişim bilgileri ve logoları buradan yönetebilirsiniz.
                                </p>
                            </div>
                            <ContentForm initialContent={mergedContents} />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}
