import { PLAN_AMOUNTS, getPlanLabel } from "@/lib/stripe/plans"
import type { Plan } from "@/types"

const base = process.env.NEXT_PUBLIC_APP_URL ?? "https://propertymanagement.network"

// Real plans + displayed prices from lib/stripe/plans.ts (single source of truth).
// Annual billing is auto-provisioned at checkout and has no fixed amount here, so
// we advertise only the monthly / one-time base prices that actually exist.
const planOrder: Plan[] = ["starter", "pro", "landlord", "lifetime"]
const planOffers = planOrder.map((plan) => ({
  "@type": "Offer",
  name: getPlanLabel(plan),
  price: String(PLAN_AMOUNTS[plan]),
  priceCurrency: "USD",
}))

// Mirrors the visible FAQ content in components/marketing/faq.tsx.
// Keep these in sync with that source so the JSON-LD matches what users see.
const faqs = [
  {
    q: "Is there really a free plan?",
    a: "Yes — the Starter plan is free forever. You get 1 property, up to 3 tenants, rent tracking, maintenance requests, and lease management. No credit card needed to sign up.",
  },
  {
    q: "What happens when my trial ends?",
    a: "There's no trial — paid plans start immediately when you upgrade. If you're on the free Starter plan, it never expires. You upgrade when you outgrow it.",
  },
  {
    q: "Can I cancel anytime?",
    a: "Yes. Cancel any time from your billing portal — no questions, no fees. Your data stays accessible for 30 days after cancellation so you can export everything.",
  },
  {
    q: "Do tenants need to create an account?",
    a: "No. Each tenant gets a unique private link to their portal — no account creation, no password. They can view rent history and submit maintenance requests instantly.",
  },
  {
    q: "Does Property Management Network handle actual rent collection?",
    a: "Yes — the Pro and Landlord plans include Stripe payment link generation. You can create a payment link for each tenant and they pay directly via card or bank transfer.",
  },
  {
    q: "Is my data secure?",
    a: "Yes. Every request is authenticated and scoped to your account, so landlords only ever see their own data and tenants only see their own records. All data is encrypted at rest and in transit.",
  },
  {
    q: "Can I manage multiple properties?",
    a: "The Starter plan supports 1 property. Pro supports up to 10. Landlord and Lifetime plans support unlimited properties and units.",
  },
  {
    q: "What's included in the Lifetime deal?",
    a: "The Lifetime plan is a one-time payment of $199. You get everything in the Landlord plan — unlimited properties, tenants, 25GB storage, AI calls, team access — with no recurring fees, ever. All future updates included.",
  },
]

const organization: Record<string, unknown> = {
  "@context": "https://schema.org",
  "@type": "Organization",
  name: "Property Management Network",
  url: base,
  logo: `${base}/logo-mark.png`,
  contactPoint: {
    "@type": "ContactPoint",
    contactType: "customer support",
    email: "support@propertymanagement.network",
  },
  sameAs: [
    "https://twitter.com/propertymgmtnet",
    "https://github.com/propertymanagement-network",
  ],
}

const website: Record<string, unknown> = {
  "@context": "https://schema.org",
  "@type": "WebSite",
  name: "Property Management Network",
  url: base,
}

const softwareApplication: Record<string, unknown> = {
  "@context": "https://schema.org",
  "@type": "SoftwareApplication",
  name: "Property Management Network",
  applicationCategory: "BusinessApplication",
  operatingSystem: "Web",
  description:
    "Property management software for independent landlords — track rent, maintenance requests, leases and expenses in one place. Free to start.",
  offers: planOffers,
}

const faqPage: Record<string, unknown> = {
  "@context": "https://schema.org",
  "@type": "FAQPage",
  mainEntity: faqs.map((faq) => ({
    "@type": "Question",
    name: faq.q,
    acceptedAnswer: {
      "@type": "Answer",
      text: faq.a,
    },
  })),
}

export function StructuredData() {
  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(organization) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(website) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(softwareApplication) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(faqPage) }}
      />
    </>
  )
}
