import type { Metadata } from "next";
import { Inter, Montserrat } from "next/font/google";
import "@/public/styles/main.scss";
import { CookieConsentProvider } from "@/components/shared/layout/CookieConsentContext";
import { CookieConsent } from "@/components/shared/layout/CookieConsent";
import LayoutWrapper from "@/components/shared/layout/LayoutWrapper";
import { generateMetadata as createMetadata } from "@/lib/seo/metadata";
import { OrganizationSchema, WebsiteSchema, LocalBusinessSchema } from "@/components/shared/seo/StructuredData";

const montserrat = Montserrat({
  subsets: ["latin"],
  display: "swap",
  weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
  variable: "--mont",
  preload: false, // Disable preload to prevent warnings
  fallback: [
    "-apple-system",
    "Segoe UI",
    "Roboto",
    "Ubuntu",
    "Fira Sans",
    "Arial",
    "sans-serif",
  ],
});

const inter = Inter({
  subsets: ["latin"],
  display: "swap",
  weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
  variable: "--inter",
  preload: false, // Disable preload to prevent warnings
  fallback: [
    "-apple-system",
    "Segoe UI",
    "Roboto",
    "Ubuntu",
    "Fira Sans",
    "Arial",
    "sans-serif",
  ],
});

// Enhanced SEO metadata for root layout
export const metadata: Metadata = createMetadata({
  title: "Enterprise Software Development & IT Solutions",
  description: "Leading enterprise software development company specializing in custom software, data replication, incident management, AI business intelligence, and comprehensive system integrations for modern businesses.",
  keywords: [
    "Enterprise Software Development",
    "Custom Software Solutions",
    "Data Replication Services",
    "Incident Management SaaS",
    "AI Business Intelligence",
    "Backend Engineering",
    "Frontend Engineering",
    "Systems Integration",
  ],
  url: "/",
});

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en" style={{ scrollBehavior: 'auto', overflow: 'auto' }}>
      <head>
        {/* Suppress scroll-linked positioning warning - expected with GSAP ScrollTrigger */}
        <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
        <script
          dangerouslySetInnerHTML={{
            __html: `
              if ('scrollRestoration' in history) {
                history.scrollRestoration = 'manual';
              }
              window.scrollTo(0, 0);
              
              // Suppress Font Awesome glyph bbox warnings (harmless font rendering warnings)
              (function() {
                const originalWarn = console.warn;
                console.warn = function(...args) {
                  const message = args.join(' ');
                  if (message.includes('downloadable font: Glyph bbox') || 
                      message.includes('Font Awesome') ||
                      message.includes('glyph ids')) {
                    return; // Suppress Font Awesome font warnings
                  }
                  originalWarn.apply(console, args);
                };
              })();
            `,
          }}
        />
        {/* Content Protection Script */}
        <script
          dangerouslySetInnerHTML={{
            __html: `
              (function() {
                if (typeof window === 'undefined') return;
                
                // Wait for DOM to be ready
                document.addEventListener('DOMContentLoaded', function() {
                  // Disable right-click
                  document.addEventListener('contextmenu', function(e) {
                    e.preventDefault();
                    return false;
                  });
                  
                  // Disable keyboard shortcuts
                  document.addEventListener('keydown', function(e) {
                    // Ctrl+C, Ctrl+X, Ctrl+S, Ctrl+A, Ctrl+P, Ctrl+U, Ctrl+I, Ctrl+J
                    if ((e.ctrlKey || e.metaKey) && ['c','x','s','a','p','u','i','j','k'].includes(e.key)) {
                      e.preventDefault();
                      return false;
                    }
                    // F12
                    if (e.key === 'F12' || e.keyCode === 123) {
                      e.preventDefault();
                      return false;
                    }
                    // Ctrl+Shift+I, Ctrl+Shift+J, Ctrl+Shift+C
                    if ((e.ctrlKey || e.metaKey) && e.shiftKey && ['I','J','C'].includes(e.key)) {
                      e.preventDefault();
                      return false;
                    }
                  });
                  
                  // Disable text selection
                  document.addEventListener('selectstart', function(e) {
                    if (e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA') {
                      e.preventDefault();
                      return false;
                    }
                  });
                  
                  // Disable image dragging
                  document.addEventListener('dragstart', function(e) {
                    e.preventDefault();
                    return false;
                  });
                  
                  // Disable copy/cut
                  document.addEventListener('copy', function(e) {
                    e.preventDefault();
                    return false;
                  });
                  document.addEventListener('cut', function(e) {
                    e.preventDefault();
                    return false;
                  });
                });
              })();
            `,
          }}
        />
      </head>
      <body className={`${inter.variable} ${montserrat.variable} content-protected`} style={{ scrollBehavior: 'auto', overflow: 'auto' }}>
        {/* Structured Data for SEO */}
        <OrganizationSchema />
        <WebsiteSchema />
        <LocalBusinessSchema />
        
        <CookieConsentProvider
          config={{
            companyName: "GNX Soft",
            privacyPolicyUrl: "/policy?type=privacy",
            cookiePolicyUrl: "/policy?type=privacy",
            dataControllerEmail: "privacy@gnxsoft.com",
            retentionPeriod: 365,
            enableAuditLog: true,
            enableDetailedSettings: true,
            showPrivacyNotice: true,
          }}
        >
          <LayoutWrapper>
            {children}
          </LayoutWrapper>
          <CookieConsent />
        </CookieConsentProvider>
      </body>
    </html>
  );
}
