/**
 * Schema Requirements Mapper
 * ----------------------------
 * Static mapping of snippet types to schema.org requirements.
 * No API calls. Pure function.
 *
 * Internal module.
 */

import type { SnippetType, CaptureSpec } from "./types";

type SchemaRequirements = CaptureSpec['schemaRequirements'];

const BASE_SCHEMA_MAP: Record<SnippetType, SchemaRequirements> = {
  paragraph: [
    {
      schemaType: "Article",
      requiredProperties: ["headline", "author", "datePublished"],
      optionalProperties: ["image", "publisher", "mainEntityOfPage"],
      rationale: "Article schema reinforces topical relevance and authorship signals for paragraph snippets.",
    },
  ],
  list_ordered: [
    {
      schemaType: "HowTo",
      requiredProperties: ["name", "step"],
      optionalProperties: ["totalTime", "tool", "supply", "estimatedCost"],
      rationale: "HowTo schema is the strongest signal for ordered list snippets, especially for procedural queries.",
    },
  ],
  list_unordered: [
    {
      schemaType: "ItemList",
      requiredProperties: ["itemListElement"],
      optionalProperties: ["numberOfItems", "itemListOrder"],
      rationale: "ItemList schema provides explicit list structure for unordered comparison and enumeration snippets.",
    },
  ],
  table: [
    {
      schemaType: "Dataset",
      requiredProperties: ["name", "description"],
      optionalProperties: ["variableMeasured", "distribution"],
      rationale: "Dataset schema signals tabular data structure; pairs with proper HTML table semantics.",
    },
  ],
  definition: [
    {
      schemaType: "DefinedTerm",
      requiredProperties: ["name", "description"],
      optionalProperties: ["termCode", "inDefinedTermSet"],
      rationale: "DefinedTerm schema explicitly marks definitional content; high capture probability for 'what is' queries.",
    },
  ],
  how_to: [
    {
      schemaType: "HowTo",
      requiredProperties: ["name", "step"],
      optionalProperties: ["totalTime", "tool", "supply", "estimatedCost", "image"],
      rationale: "HowTo schema is the dominant capture signal for procedural and instructional snippets.",
    },
  ],
  faq: [
    {
      schemaType: "FAQPage",
      requiredProperties: ["mainEntity"],
      optionalProperties: [],
      rationale: "FAQPage schema with Question and Answer subtypes is required for People Also Ask and FAQ snippet capture.",
    },
  ],
  video: [
    {
      schemaType: "VideoObject",
      requiredProperties: ["name", "description", "thumbnailUrl", "uploadDate"],
      optionalProperties: ["duration", "contentUrl", "embedUrl", "transcript"],
      rationale: "VideoObject schema with transcript is required for video snippet capture in search results.",
    },
  ],
};

const VERTICAL_ADDITIONS: Record<string, Partial<Record<SnippetType, SchemaRequirements>>> = {
  "healthcare-medical": {
    paragraph: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy", "medicalSpecialty"],
        rationale: "MedicalWebPage schema is required for medical advice queries to signal clinical accuracy and audience relevance.",
      },
    ],
    definition: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy"],
        rationale: "Medical definition queries require MedicalWebPage schema to establish clinical credibility.",
      },
    ],
    how_to: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy"],
        rationale: "Medical procedural content requires MedicalWebPage schema for health-related how-to queries.",
      },
    ],
    faq: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy"],
        rationale: "Medical FAQ content benefits from MedicalWebPage schema alongside FAQPage for health queries.",
      },
    ],
    list_ordered: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy"],
        rationale: "Medical step-by-step content requires MedicalWebPage schema for clinical context.",
      },
    ],
    list_unordered: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed"],
        rationale: "Medical list content benefits from MedicalWebPage schema for health queries.",
      },
    ],
    table: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed"],
        rationale: "Medical comparison tables require MedicalWebPage schema for health-related queries.",
      },
    ],
    video: [
      {
        schemaType: "MedicalWebPage",
        requiredProperties: ["name", "medicalAudience"],
        optionalProperties: ["lastReviewed", "reviewedBy"],
        rationale: "Medical video content requires MedicalWebPage schema for health query eligibility.",
      },
    ],
  },
  "healthcare-dental": {
    paragraph: [{ schemaType: "MedicalWebPage", requiredProperties: ["name", "medicalAudience"], optionalProperties: ["lastReviewed", "reviewedBy"], rationale: "MedicalWebPage schema is required for dental advice queries to signal clinical accuracy." }],
    definition: [{ schemaType: "MedicalWebPage", requiredProperties: ["name", "medicalAudience"], optionalProperties: ["lastReviewed"], rationale: "Dental definition queries require MedicalWebPage schema for clinical credibility." }],
    how_to: [{ schemaType: "MedicalWebPage", requiredProperties: ["name", "medicalAudience"], optionalProperties: ["lastReviewed"], rationale: "Dental procedural content requires MedicalWebPage schema." }],
    faq: [{ schemaType: "MedicalWebPage", requiredProperties: ["name", "medicalAudience"], optionalProperties: ["lastReviewed"], rationale: "Dental FAQ content benefits from MedicalWebPage schema alongside FAQPage." }],
  },
  "local-services": {
    how_to: [
      {
        schemaType: "LocalBusiness",
        requiredProperties: ["name", "address", "telephone"],
        optionalProperties: ["openingHours", "geo", "areaServed"],
        rationale: "LocalBusiness schema on how-to pages reinforces geographic relevance for local service queries.",
      },
    ],
    paragraph: [
      {
        schemaType: "LocalBusiness",
        requiredProperties: ["name", "address", "telephone"],
        optionalProperties: ["openingHours", "geo", "areaServed"],
        rationale: "LocalBusiness schema reinforces geographic authority for local service snippet capture.",
      },
    ],
  },
  ecommerce: {
    table: [
      {
        schemaType: "Product",
        requiredProperties: ["name", "offers"],
        optionalProperties: ["aggregateRating", "brand", "image", "description"],
        rationale: "Product schema on comparison tables enables rich product results alongside table snippets.",
      },
    ],
    list_unordered: [
      {
        schemaType: "Product",
        requiredProperties: ["name", "offers"],
        optionalProperties: ["aggregateRating", "brand"],
        rationale: "Product schema on list pages enables rich results for product listing queries.",
      },
    ],
  },
  "b2b-saas": {
    definition: [
      {
        schemaType: "SoftwareApplication",
        requiredProperties: ["name", "applicationCategory", "operatingSystem"],
        optionalProperties: ["offers", "aggregateRating", "featureList"],
        rationale: "SoftwareApplication schema on definitional pages establishes category authority for B2B SaaS queries.",
      },
    ],
    table: [
      {
        schemaType: "SoftwareApplication",
        requiredProperties: ["name", "applicationCategory"],
        optionalProperties: ["offers", "featureList", "aggregateRating"],
        rationale: "SoftwareApplication schema on comparison tables signals product relevance for SaaS evaluation queries.",
      },
    ],
  },
};

export function getSchemaRequirementsForSnippetType(
  snippetType: SnippetType,
  vertical: string,
): SchemaRequirements {
  const base = [...(BASE_SCHEMA_MAP[snippetType] ?? [])];
  const verticalOverrides = VERTICAL_ADDITIONS[vertical];
  if (verticalOverrides) {
    const additions = verticalOverrides[snippetType] ?? [];
    base.push(...additions);
  }
  return base;
}
