---
// SEO Component for page-specific structured data
// Use in the head slot of BaseLayout for additional schema markup

interface BreadcrumbItem {
  name: string;
  url: string;
}

interface Props {
  type?: 'WebPage' | 'BlogPosting' | 'Article' | 'Service' | 'AboutPage' | 'ContactPage' | 'FAQPage';
  // When true this instance emits ONLY the site-wide base schema
  // (Organization + LocalBusiness and WebSite). Rendered once from
  // BaseLayout's <head> so every route carries the canonical company data,
  // independently of whether the page adds its own page-specific <SEO>.
  base?: boolean;
  breadcrumbs?: BreadcrumbItem[];
  article?: {
    author: string;
    datePublished: string;
    dateModified?: string;
    category?: string;
    tags?: string[];
    image?: string;
    wordCount?: number;
  };
  service?: {
    name: string;
    description: string;
  };
  faq?: Array<{
    question: string;
    answer: string;
  }>;
}

const { type = 'WebPage', base = false, breadcrumbs, article, service, faq } = Astro.props;

const baseUrl = 'https://workroot.in';
const currentUrl = Astro.url.href;

const orgName = 'WorkRoot IT Solutions LLP';
const orgId = `${baseUrl}/#organization`;

// ---------------------------------------------------------------------------
// Site-wide base schema — Organization + LocalBusiness and WebSite.
//
// Emitted once per page via `<SEO base />` rendered from BaseLayout's <head>,
// so every route (including pages without their own page-specific <SEO>)
// carries WorkRoot's canonical structured data.
//
// `review` / `aggregateRating` are built from the three real, attributable
// client testimonials shown on the homepage (see the `testimonials` array in
// src/pages/index.astro). All three are 5★, so the aggregate rating is 5.0
// over reviewCount = 3.
//
// Headline proof numbers in the description (20+ projects · 11+ districts ·
// 50,000+ records · 99.9% uptime) mirror the canonical set in
// ProofNumbers.astro and the /services stats — keep them identical site-wide.
// ---------------------------------------------------------------------------
const testimonialReviews = [
  {
    author: 'Ajay Pruthi',
    body: 'WorkRoot IT Solutions is very professional and responsive. They built PLNR Financing platform with great attention to detail. The Angular + Spring Boot stack works flawlessly and their ongoing support is excellent.',
  },
  {
    author: 'Dhirendra Singh',
    body: 'The FPC Cart e-commerce platform delivered by WorkRoot exceeded our expectations. Built with Astro and Golang, it loads incredibly fast and handles our product catalog seamlessly. Their team understood our business needs perfectly.',
  },
  {
    author: 'Dilip Raj',
    body: 'WorkRoot delivered our University Management System for IIT Patna on time and within budget. The Next.js + Spring Boot architecture is robust and scalable. They are a reliable technology partner for complex enterprise projects.',
  },
];

const reviewNodes = testimonialReviews.map((review) => ({
  "@type": "Review",
  "author": {
    "@type": "Person",
    "name": review.author
  },
  "reviewRating": {
    "@type": "Rating",
    "ratingValue": "5",
    "bestRating": "5",
    "worstRating": "1"
  },
  "reviewBody": review.body
}));

const organizationSchema = {
  "@context": "https://schema.org",
  "@type": ["Organization", "LocalBusiness"],
  "@id": orgId,
  "name": orgName,
  "alternateName": ["WorkRoot", "WorkRoot IT Solutions"],
  "url": baseUrl,
  "logo": {
    "@type": "ImageObject",
    "url": `${baseUrl}/logo.png`,
    "width": "512",
    "height": "512"
  },
  "image": `${baseUrl}/og-image.jpg`,
  "description": "WorkRoot IT Solutions LLP builds production-grade web, mobile, ERP, and government IT systems — 20+ projects delivered across 11+ districts, 50,000+ records managed, and 99.9% uptime on mission-critical government deployments.",
  "foundingDate": "2022",
  "founder": {
    "@type": "Person",
    "name": "Ajay Ghanwat"
  },
  "address": {
    "@type": "PostalAddress",
    "streetAddress": "At Post Rajapur Shantinagar, Taluka Khatav",
    "addressLocality": "Satara",
    "addressRegion": "MH",
    "postalCode": "415503",
    "addressCountry": "IN"
  },
  "geo": {
    "@type": "GeoCoordinates",
    "latitude": "17.7733632",
    "longitude": "74.3309312"
  },
  "hasMap": "https://maps.app.goo.gl/9bZ5ez1pjbU74ATq5",
  "telephone": "+91-9561417403",
  "email": "admin@workroot.in",
  "contactPoint": [{
    "@type": "ContactPoint",
    "telephone": "+91-9561417403",
    "contactType": "customer service",
    "email": "admin@workroot.in",
    "availableLanguage": ["English", "Hindi", "Marathi"],
    "areaServed": "IN"
  }, {
    "@type": "ContactPoint",
    "telephone": "+91-9561417403",
    "contactType": "sales",
    "email": "admin@workroot.in",
    "availableLanguage": ["English", "Hindi", "Marathi"],
    "areaServed": "IN"
  }],
  "openingHoursSpecification": [
    {
      "@type": "OpeningHoursSpecification",
      "dayOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
      "opens": "10:00",
      "closes": "18:00"
    }
  ],
  "priceRange": "$$",
  "currenciesAccepted": "INR",
  "paymentAccepted": "Cash, Credit Card, Bank Transfer",
  "sameAs": [
    "https://x.com/GhanwatAjay",
    "https://www.linkedin.com/company/workroot",
    "https://git.workroot.in/WorkRoot/",
    "https://www.facebook.com/profile.php?id=61576503897897"
  ],
  "areaServed": [
    {
      "@type": "AdministrativeArea",
      "name": "Bihar"
    },
    {
      "@type": "Country",
      "name": "India"
    }
  ],
  "serviceType": ["Web Development", "Mobile App Development", "ERP & Enterprise Systems", "Government IT Solutions"],
  "knowsAbout": ["Web Development", "Software Engineering", "Cloud Computing", "Artificial Intelligence", "Machine Learning", "Mobile Development"],
  "slogan": "Transforming Ideas Into Digital Reality",
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "5.0",
    "reviewCount": String(reviewNodes.length),
    "bestRating": "5",
    "worstRating": "1"
  },
  "review": reviewNodes
};

const websiteSchema = {
  "@context": "https://schema.org",
  "@type": "WebSite",
  "name": orgName,
  "url": baseUrl,
  "description": "Professional IT services including web development, cloud solutions, and digital transformation for businesses.",
  "publisher": {
    "@type": "Organization",
    "@id": orgId,
    "name": orgName
  },
  "potentialAction": {
    "@type": "SearchAction",
    "target": {
      "@type": "EntryPoint",
      "urlTemplate": "https://workroot.in/blog?search={search_term_string}"
    },
    "query-input": "required name=search_term_string"
  },
  "inLanguage": "en-US"
};

// Get page title and description from parent layout props
const pageTitle = (Astro.props as any).title || 'WorkRoot IT Solutions';
const pageDescription = (Astro.props as any).description || 'Professional IT services including web development, cloud solutions, and digital transformation for businesses.';

// Generate BreadcrumbList schema
const breadcrumbSchema = breadcrumbs ? {
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": breadcrumbs.map((item, index) => ({
    "@type": "ListItem",
    "position": index + 1,
    "name": item.name,
    "item": `${baseUrl}${item.url}`
  }))
} : null;

// Generate BlogPosting schema for blog posts (more specific than Article)
const blogPostingSchema = article && type === 'BlogPosting' ? {
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": pageTitle,
  "description": pageDescription,
  "image": article.image ? {
    "@type": "ImageObject",
    "url": article.image,
    "width": 1200,
    "height": 675
  } : undefined,
  "author": {
    "@type": "Person",
    "name": article.author,
    "url": `${baseUrl}/about`
  },
  "datePublished": article.datePublished,
  "dateModified": article.dateModified || article.datePublished,
  "publisher": {
    "@type": "Organization",
    "name": "WorkRoot IT Solutions LLP",
    "url": baseUrl,
    "logo": {
      "@type": "ImageObject",
      "url": `${baseUrl}/logo.png`,
      "width": 512,
      "height": 512
    }
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": currentUrl
  },
  "keywords": article.tags?.join(', '),
  "articleSection": article.category,
  "wordCount": article.wordCount,
  "inLanguage": "en-US"
} : null;

// Generate Service schema — reusable node; each service page passes its own
// name/description via the `service` prop. Provider is linked by @id to the
// site-wide Organization node so the structured-data graph stays connected.
const serviceSchema = service ? {
  "@context": "https://schema.org",
  "@type": "Service",
  "name": service.name,
  "description": service.description,
  "provider": {
    "@type": "Organization",
    "@id": orgId,
    "name": orgName,
    "url": baseUrl
  },
  "areaServed": [
    {
      "@type": "AdministrativeArea",
      "name": "Bihar"
    },
    {
      "@type": "Country",
      "name": "India"
    }
  ],
  "serviceType": service.name
} : null;

// Generate WebPage schema for standard pages
const webPageSchema = type === 'WebPage' && Astro.url.pathname !== '/' ? {
  "@context": "https://schema.org",
  "@type": "WebPage",
  "name": pageTitle,
  "description": pageDescription,
  "url": currentUrl,
  "inLanguage": "en-US",
  "isPartOf": {
    "@type": "WebSite",
    "@id": `${baseUrl}/#website`
  },
  "about": {
    "@type": "Organization",
    "name": "WorkRoot IT Solutions LLP"
  },
  "primaryImageOfPage": {
    "@type": "ImageObject",
    "url": `${baseUrl}/og-image.jpg`
  }
} : null;

// Generate FAQPage schema
const faqPageSchema = faq && faq.length > 0 ? {
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": faq.map((item) => ({
    "@type": "Question",
    "name": item.question,
    "acceptedAnswer": {
      "@type": "Answer",
      "text": item.answer
    }
  }))
} : null;

// Generate AboutPage schema
const aboutPageSchema = type === 'AboutPage' ? {
  "@context": "https://schema.org",
  "@type": "AboutPage",
  "name": pageTitle,
  "description": pageDescription,
  "url": currentUrl,
  "inLanguage": "en-US",
  "mainEntity": {
    "@type": "Organization",
    "name": "WorkRoot IT Solutions LLP",
    "url": baseUrl
  }
} : null;

// Generate ContactPage schema with LocalBusiness details
const contactPageSchema = type === 'ContactPage' ? {
  "@context": "https://schema.org",
  "@type": "ContactPage",
  "name": pageTitle,
  "description": pageDescription,
  "url": currentUrl,
  "inLanguage": "en-US",
  "mainEntity": {
    "@type": "LocalBusiness",
    "name": "WorkRoot IT Solutions LLP",
    "alternateName": "WorkRoot IT Solutions",
    "url": baseUrl,
    "telephone": "+91-9561417403",
    "email": "admin@workroot.in",
    "address": {
      "@type": "PostalAddress",
      "streetAddress": "At Post Rajapur Shantinagar, Taluka Khatav",
      "addressLocality": "Satara",
      "addressRegion": "MH",
      "postalCode": "415503",
      "addressCountry": "IN"
    },
    "geo": {
      "@type": "GeoCoordinates",
      "latitude": "17.7733632",
      "longitude": "74.3309312"
    },
    "openingHoursSpecification": [
      {
        "@type": "OpeningHoursSpecification",
        "dayOfWeek": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
        "opens": "09:00",
        "closes": "18:00"
      }
    ],
    "priceRange": "$$",
    "currenciesAccepted": "INR",
    "paymentAccepted": "Cash, Credit Card, Bank Transfer",
    "areaServed": "Worldwide",
    "image": `${baseUrl}/og-image.jpg`,
    "logo": {
      "@type": "ImageObject",
      "url": `${baseUrl}/logo.png`,
      "width": 512,
      "height": 512
    },
    "hasMap": "https://maps.google.com/?q=Rajapur+Shantinagar+Khatav+Satara+Maharashtra+415503",
    "sameAs": [
      "https://twitter.com/workroot",
      "https://linkedin.com/company/workroot",
      "https://github.com/workroot",
      "https://facebook.com/workroot"
    ]
  }
} : null;
---

{/* Site-wide base schema — emitted only by the single `<SEO base />` rendered
    from BaseLayout's <head>, so it appears exactly once on every page. */}
{base && (
  <script type="application/ld+json" set:html={JSON.stringify(organizationSchema)} />
)}

{base && (
  <script type="application/ld+json" set:html={JSON.stringify(websiteSchema)} />
)}

{/* Page-specific schema — never emitted by the base instance, so there is no
    duplication with the site-wide Organization/WebSite nodes above. */}
{!base && breadcrumbSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(breadcrumbSchema)} />
)}

{!base && blogPostingSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(blogPostingSchema)} />
)}

{!base && serviceSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(serviceSchema)} />
)}

{!base && webPageSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(webPageSchema)} />
)}

{!base && faqPageSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(faqPageSchema)} />
)}

{!base && aboutPageSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(aboutPageSchema)} />
)}

{!base && contactPageSchema && (
  <script type="application/ld+json" set:html={JSON.stringify(contactPageSchema)} />
)}
