"use client";
import { poppins } from "@/lib/font";
import "@/node_modules/react-modal-video/css/modal-video.css";
import "../public/assets/css/bootstrap.css";
import "../public/assets/css/color.css";
import "../public/assets/css/style.css";
import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/free-mode";
import "../public/assets/css/google-reviews.css";
import Script from "next/script";
import { usePathname } from "next/navigation";

export default function RootLayout({ children }) {
  const pathname = usePathname();
  const siteUrl = "https://rapharehab.ca";
  const normalizedPath = pathname.endsWith('/') ? pathname : `${pathname}/`;
  const canonicalUrl = `${siteUrl}${normalizedPath}`;
  const logoUrl = `${siteUrl}/assets/images/logo.png`;
  const ogImage = `${siteUrl}/assets/images/logo.jpg`;

  const pageTitle = "Physiotherapy Clinic in Etobicoke | Rapha";
  const pageDescription = " Expert physiotherapy in Etobicoke for injury, pain relief & rehab. Personalized treatment plans. Book your appointment today.";

  return (
    <html lang="en" className={`${poppins.variable}`}>
      <head>
        {/* <title>{pageTitle}</title>
        <meta name="description" content={pageDescription} /> */}
        {pathname === "/" && (
          <>
            <title>{pageTitle}</title>
            <meta name="description" content={pageDescription} />
          </>
        )}
        {/* ✅ Preload critical font */}
        {/* <link
          rel="preload"
          href="/fonts/poppins.woff2"
          as="font"
          type="font/woff2"
          crossOrigin="anonymous"
        /> */}

        {/* ✅ Canonical Tag */}
        <link rel="canonical" href={canonicalUrl} aria-label="Site Url" />
        <meta name="robots" content="index, follow" />

        {/* ✅ Open Graph Meta */}
        <meta property="og:title" content="Rapharehab | Physiotherapy, Massage & Rehab Clinic in Etobicoke, ON" />
        <meta
          property="og:description"
          content="Trusted physiotherapy, pain relief and rehab clinic in Etobicoke. Book your appointment now."
        />
        <meta property="og:type" content="website" />
        <meta property="og:url" content={canonicalUrl} />
        <meta property="og:image" content={ogImage} />
        <meta property="og:site_name" content="Rapharehab" />
        <meta property="og:locale" content="en_CA" />

        {/* ✅ Twitter Meta */}
        <meta name="twitter:card" content="summary_large_image" />
        <meta
          name="twitter:title"
          content="Rapharehab – Physiotherapy & Rehab Clinic in Etobicoke"
        />
        <meta
          name="twitter:description"
          content="Expert physiotherapy and pain relief treatments tailored to you."
        />
        <meta name="twitter:image" content={ogImage} />
        <meta name="twitter:site" content="@YourTwitterHandle" />

        {/* ✅ Preconnect — Faster 3rd-party requests */}
        <link rel="preconnect" href="https://www.googletagmanager.com" aria-label="Googleletamanage" />
        <link rel="preconnect" href="https://connect.facebook.net" aria-label="facebook" />
        <link rel="preconnect" href="https://www.facebook.com" aria-label="Facebook" />
        <link rel="preconnect" href="https://scripts.clarity.ms" aria-label="Scripts Clarity" />

        {/* ✅ fetchpriority for LCP Image */}
        <link
          rel="preload"
          as="image"
          href="/assets/images/banner/mobile-banner/4.webp"
          fetchPriority="high"
          aria-label="loader"
        />

        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{
            __html: JSON.stringify({
              "@context": "https://schema.org",
              "@type": "PhysiotherapyClinic",
              "name": "Rapharehab Clinic",
              "image": logoUrl,
              "@id": `${siteUrl}/#clinic`,
              "url": siteUrl,
              "telephone": "+16477223434",
              "priceRange": "$$",
              "medicalSpecialty": "PhysicalTherapy",
              "address": {
                "@type": "PostalAddress",
                "streetAddress": "5 – 4335 Bloor Street West",
                "addressLocality": "Etobicoke",
                "addressRegion": "ON",
                "postalCode": "M9C 2A5",
                "addressCountry": "CA"
              },
              "geo": {
                "@type": "GeoCoordinates",
                "latitude": 43.6389,
                "longitude": -79.5716
              },
              "openingHoursSpecification": [
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Monday",
                  "opens": "10:00",
                  "closes": "20:00"
                },
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Tuesday",
                  "opens": "10:00",
                  "closes": "20:00"
                },
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Wednesday",
                  "opens": "10:00",
                  "closes": "20:00"
                },
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Thursday",
                  "opens": "10:00",
                  "closes": "20:00"
                },
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Friday",
                  "opens": "10:00",
                  "closes": "20:00"
                },
                {
                  "@type": "OpeningHoursSpecification",
                  "dayOfWeek": "Saturday",
                  "opens": "10:00",
                  "closes": "17:00"
                }
              ],
              "sameAs": [
                "https://www.facebook.com/ELRaphaRehabCenter/",
                "https://www.instagram.com/elrapharehab/"
              ]
            })
          }}
        />

        <script
          dangerouslySetInnerHTML={{
            __html: `!function(w,d){if(!w.rdt){var p=w.rdt=function(){p.sendEvent?p.sendEvent.apply(p,arguments):p.callQueue.push(arguments)};p.callQueue=[];var t=d.createElement("script");t.src="https://www.redditstatic.com/ads/pixel.js",t.async=!0;var s=d.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}}(window,document);rdt('init','a2_ialvabk9p9uj');rdt('track', 'PageVisit');`,
          }}
        />


      </head>
      <body>
        {/* PhysiotherapyClinic Schema Markup */}
        {/* <Script
          id="schema-physiotherapy"
          type="application/ld+json"
          strategy="afterInteractive"
        >
          {JSON.stringify({
            "@context": "https://schema.org",
            "@type": "PhysiotherapyClinic",
            name: "Rapharehab Clinic",
            image: logoUrl,
            "@id": siteUrl,
            url: siteUrl,
            telephone: "+16477223434",
            priceRange: "$$",
            medicalSpecialty: "PhysicalTherapy",
            address: {
              "@type": "PostalAddress",
              streetAddress: "5 – 4335 Bloor Street West",
              addressLocality: "Etobicoke",
              addressRegion: "ON",
              postalCode: "M9C 2A5",
              addressCountry: "CA",
            },
            geo: {
              "@type": "GeoCoordinates",
              latitude: 43.6389,
              longitude: -79.5716,
            },
            openingHoursSpecification: [
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Monday",
                opens: "10:00",
                closes: "20:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Tuesday",
                opens: "14:00",
                closes: "19:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Wednesday",
                opens: "10:00",
                closes: "20:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Thursday",
                opens: "10:00",
                closes: "20:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Friday",
                opens: "10:00",
                closes: "20:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Saturday",
                opens: "10:00",
                closes: "19:00",
              },
              {
                "@type": "OpeningHoursSpecification",
                dayOfWeek: "Sunday",
                opens: "00:00",
                closes: "00:00",
                closed: true,
              },
            ],
            sameAs: [
              "https://www.facebook.com/ELRaphaRehabCenter/",
              "https://www.instagram.com/elrapharehab/",
            ],
          })}
        </Script> */}

        {/* ✅ Google Tag Manager — non-blocking */}
        <Script id="gtm-init" strategy="afterInteractive">
          {`
            (function(w,d,s,l,i){
              w[l]=w[l]||[];
              w[l].push({'gtm.start': new Date().getTime(), event:'gtm.js'});
              var f=d.getElementsByTagName(s)[0],
              j=d.createElement(s),
              dl=l!='dataLayer'?'&l='+l:'';
              j.async=true;
              j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
              f.parentNode.insertBefore(j,f);
            })(window,document,'script','dataLayer','G-KM1KJW539N');
          `}
        </Script>

        {/* Google Analytics */}
        <Script
          async
          src="https://www.googletagmanager.com/gtag/js?id=G-KM1KJW539N"
        />
        <Script id="google-analytics">
          {`
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', 'G-KM1KJW539N');
          `}
        </Script>



        {/* ✅ Microsoft Clarity — lazy load to reduce LCP impact */}
        <Script id="clarity-script" strategy="lazyOnload">
          {`
            window.onload = function() {
              setTimeout(() => {
                (function(c,l,a,r,i,t,y){
                  c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
                  t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/th7p0lr1ca";
                  y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
                })(window, document, "clarity", "script", "th7p0lr1ca");
              }, 3000);
            };
          `}
        </Script>

        {/* ✅ Facebook Pixel — lazy load to avoid blocking */}
        <Script id="facebook-pixel" strategy="lazyOnload">
          {`
            setTimeout(() => {
              !function(f,b,e,v,n,t,s){
                if(f.fbq)return;
                n=f.fbq=function(){n.callMethod?
                n.callMethod.apply(n,arguments):n.queue.push(arguments)};
                if(!f._fbq)f._fbq=n;
                n.push=n;
                n.loaded=!0;
                n.version='2.0';
                n.queue=[];
                t=b.createElement(e);t.async=!0;
                t.src=v;
                s=b.getElementsByTagName(e)[0];
                s.parentNode.insertBefore(t,s)
              }(window, document,'script','https://connect.facebook.net/en_US/fbevents.js');
              fbq('init', '1133141855537200');
              fbq('track', 'PageView');
            }, 4000);
          `}
        </Script>

        {/* ✅ Facebook & GTM noscript fallback */}
        <noscript>
          {/* <iframe
            src="https://www.googletagmanager.com/ns.html?id=G-KM1KJW539N"
            height="0"
            width="0"
            style={{ display: "none", visibility: "hidden" }}
          ></iframe> */}
          <img
            height="1"
            width="1"
            style={{ display: "none" }}
            src="https://www.facebook.com/tr?id=1133141855537200&ev=PageView&noscript=1"
            alt="facebook pixel"
          />
        </noscript>

        {children}
      </body>
    </html>
  );
}
