import type { Metadata } from "next";
import { Martian_Mono } from "next/font/google";
import "./globals.css";
import ClientWrapper from "@/components/ClientWrapper";

const martianMono = Martian_Mono({
    variable: "--font-martian",
    subsets: ["latin"],
    weight: ["100", "200", "300", "400", "500", "600", "700", "800"],
    display: "swap",
});

import sql from "@/lib/db";

export async function generateMetadata(): Promise<Metadata> {
    let settings: any = null;
    try {
        const result = await sql`SELECT * FROM settings WHERE id = 1 LIMIT 1`;
        if (result.length > 0) settings = result[0];
    } catch (e) {
        console.error(e);
    }
    const siteName = settings?.site_name || "Muğla Dijital | Medya & Prodüksiyon Ajansı";
    const siteDesc = settings?.site_description || "Muğla'nın öncü dijital medya ajansı. Drone çekimi, sosyal medya yönetimi ve profesyonel reklam çözümleri.";
    const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://mugladijitalmedya.com";

    return {
        metadataBase: new URL(baseUrl),
        title: {
            default: siteName,
            template: `%s | ${siteName.split('|')[0].trim()}`
        },
        description: siteDesc,
        authors: [{ name: "Muğla Dijital" }],
        creator: "Muğla Dijital",
        publisher: "Muğla Dijital",
        alternates: {
            canonical: "/",
        },
        openGraph: {
            type: "website",
            locale: "tr_TR",
            url: baseUrl,
            siteName: "Muğla Dijital",
            title: siteName,
            description: siteDesc,
            images: [
                {
                    url: "/og-image.jpg",
                    width: 1200,
                    height: 630,
                    alt: "Muğla Dijital Medya",
                },
            ],
        },
        twitter: {
            card: "summary_large_image",
            title: siteName,
            description: siteDesc,
            images: ["/og-image.jpg"],
        },
        robots: {
            index: true,
            follow: true,
        }
    };
}

export default async function RootLayout({
    children,
}: Readonly<{
    children: React.ReactNode;
}>) {
    let settings: any = null;
    try {
        const result = await sql`SELECT * FROM settings WHERE id = 1 LIMIT 1`;
        if (result.length > 0) settings = result[0];
    } catch (e) {
        console.error(e);
    }

    const jsonLd = {
        "@context": "https://schema.org",
        "@type": "Organization",
        "name": settings?.site_name || "Muğla Dijital Medya Ajansı",
        "url": "https://mugladijitalmedya.com",
        "logo": "https://mugladijitalmedya.com/logo.png",
        "sameAs": [
            settings?.instagram_url,
            settings?.twitter_url,
            settings?.linkedin_url
        ].filter(Boolean),
        "contactPoint": {
            "@type": "ContactPoint",
            "telephone": settings?.contact_phone || "+90-252-XXX-XXXX",
            "contactType": "customer service",
            "areaServed": "TR",
            "availableLanguage": ["Turkish", "English"]
        }
    };

    const localBusinessLd = {
        "@context": "https://schema.org",
        "@type": "LocalBusiness",
        "name": settings?.site_name || "Muğla Dijital Medya Ajansı",
        "image": "https://mugladijitalmedya.com/og-image.jpg",
        "@id": "https://mugladijitalmedya.com",
        "url": "https://mugladijitalmedya.com",
        "telephone": settings?.contact_phone || "+90-252-XXX-XXXX",
        "address": {
            "@type": "PostalAddress",
            "streetAddress": settings?.office_address || "Muğla",
            "addressLocality": "Muğla",
            "addressRegion": "Muğla",
            "postalCode": "48000",
            "addressCountry": "TR"
        },
        "geo": {
            "@type": "GeoCoordinates",
            "latitude": 37.2153,
            "longitude": 28.3636
        },
        "openingHoursSpecification": {
            "@type": "OpeningHoursSpecification",
            "dayOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
            "opens": "09:00",
            "closes": "18:00"
        }
    };

    return (
        <html lang="tr">
            <body className={`${martianMono.variable} antialiased`}>
                <ClientWrapper>
                    {children}
                </ClientWrapper>
                <script
                    type="application/ld+json"
                    dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
                />
                <script
                    type="application/ld+json"
                    dangerouslySetInnerHTML={{ __html: JSON.stringify(localBusinessLd) }}
                />
            </body>
        </html>
    );
}