import type { Metadata } from "next";
export const revalidate = 0;
import "./globals.css";
import Navbar from "@/components/Navbar";
import {
    Facebook, Youtube, Linkedin, Github, MessageSquare, ExternalLink, Music
} from "lucide-react";
import prisma from "@/lib/db";
import Footer from "@/components/Footer";

export async function generateMetadata(): Promise<Metadata> {
    let settings: any = null;
    try {
        settings = await prisma.settings.findUnique({ where: { id: "global" } });
    } catch (e) {
        console.error("Metadata fetch error:", e);
    }

    return {
        metadataBase: new URL(process.env.NEXT_PUBLIC_SITE_URL || "https://metalab.africa"),
        title: settings?.siteTitle || "Sergio Hounkpessode | Technologue & Fondateur Meta Lab Africa",
        description: settings?.siteDesc || "Portfolio professionnel de Sergio Hounkpessode, expert en IoT, systèmes embarqués et développement fullstack. Fondateur de Meta Lab Africa au Bénin.",
        keywords: ["IoT", "Électronique", "Bénin", "Meta Lab Africa", "ESP32", "Next.js", "Sergio Hounkpessode"],
        alternates: {
            canonical: "/",
        }
    };
}

const footerLinks = [
    { label: "Accueil", href: "/" },
    { label: "Projets", href: "/projects" },
    { label: "À Propos", href: "/about" },
    { label: "Blog", href: "/blog" },
    { label: "Contact", href: "/contact" },
];

import { Providers } from "@/components/Providers";

export default async function RootLayout({ children }: { children: React.ReactNode }) {
    let settings: any = null;
    let profile: any = null;

    try {
        settings = await prisma.settings.findUnique({ where: { id: "global" } });
        profile = (await prisma.profile.findFirst()) as any;
    } catch (error) {
        console.error("Database connection error in RootLayout:", error);
    }

    const socialLinks = [
        { icon: <Facebook size={16} />, href: profile?.facebook, label: "Facebook" },
        { icon: <Youtube size={16} />, href: profile?.youtube, label: "Youtube" },
        { icon: <Linkedin size={16} />, href: profile?.linkedin, label: "Linkedin" },
        { icon: <Github size={16} />, href: profile?.github, label: "Github" },
        { icon: <MessageSquare size={16} />, href: profile?.whatsapp ? `https://wa.me/${profile.whatsapp.replace(/[^0-9]/g, "")}` : null, label: "WhatsApp" },
        { icon: <ExternalLink size={16} />, href: profile?.freelance, label: "Freelance" },
        { icon: <Music size={16} />, href: profile?.tiktok, label: "TikTok" },
    ].filter(s => !!s.href);

    return (
        <html lang="fr" suppressHydrationWarning>
            <body suppressHydrationWarning>
                <Providers>
                    <Navbar siteName={settings?.siteName || "Sergio"} />
                    <main>{children}</main>
                    <Footer
                        settings={settings}
                        profile={profile}
                        footerLinks={footerLinks}
                        socialLinks={socialLinks}
                    />
                </Providers>
            </body>
        </html>
    );
}
