import { Metadata } from "next"
import { getPrivacyPolicyData } from "@/lib/api/privacy-policy"

type RichTextChild = {
  text?: string
  bold?: boolean
}

type RichTextBlock = {
  type?: string
  level?: number
  children?: RichTextChild[]
}

export const metadata: Metadata = {
  title: "Privacy Policy | MGM Group",
  description: "Privacy Policy for MGM Group.",
}

function Heading({ level, children }: { level?: number; children: React.ReactNode }) {
  const className = `font-host-grotesk font-bold text-slate-800 mt-8 mb-4 ${level === 1 ? 'text-4xl' : level === 2 ? 'text-3xl' : 'text-2xl'}`
  if (level === 1) return <h1 className={className}>{children}</h1>
  if (level === 2) return <h2 className={className}>{children}</h2>
  if (level === 3) return <h3 className={className}>{children}</h3>
  return <h4 className={className}>{children}</h4>
}

function renderBlocks(blocks: RichTextBlock[]) {
  if (!Array.isArray(blocks)) return null

  return blocks.map((block, index) => {
    if (block.type === 'paragraph') {
      return (
        <p key={index} className="mb-6 text-gray-700 leading-relaxed font-manrope">
          {block.children?.map((child, childIndex) => (
            <span key={childIndex} className={child.bold ? "font-bold" : ""}>
              {child.text}
            </span>
          ))}
        </p>
      )
    }

    if (block.type === 'heading') {
      return (
        <Heading key={index} level={block.level}>
          {block.children?.map((child) => child.text).join("")}
        </Heading>
      )
    }

    return null
  })
}

export default async function PrivacyPolicyPage() {
  const data = await getPrivacyPolicyData()

  return (
    <main className="min-h-screen bg-[#f4f7fb] pt-32 pb-24">
      <div className="container mx-auto px-6 max-w-4xl bg-white rounded-[24px] shadow-sm p-8 md:p-12 lg:p-16">
        <h1 className="text-4xl md:text-5xl font-host-grotesk font-bold text-slate-900 mb-10 border-b pb-6">
          {data.title}
        </h1>

        <div className="prose prose-lg max-w-none text-slate-700 font-manrope">
          {data.contentBlocks && data.contentBlocks.length > 0 ? (
            renderBlocks(data.contentBlocks)
          ) : (
            <>
              <p className="mb-6 leading-relaxed">Last updated: June 30, 2026</p>
              <p className="mb-6 leading-relaxed">
                At MGM Group, we are deeply committed to safeguarding the privacy and security of our users and partners. This Privacy Policy outlines how we collect, use, and protect your personal information when you interact with our services, websites, and affiliated businesses.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">1. Information We Collect</h2>
              <p className="mb-6 leading-relaxed">
                We may collect personal information such as your name, email address, phone number, and company details when you voluntarily provide it through contact forms, newsletters, or business inquiries. Additionally, we collect non-personal data, including browser type, IP address, and navigation patterns, to enhance our website functionality.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">2. How We Use Your Information</h2>
              <p className="mb-6 leading-relaxed">The information we collect is primarily used to:</p>
              <ul className="list-disc pl-6 mb-6 space-y-2">
                <li>Respond to your inquiries and provide customer support.</li>
                <li>Improve our website, services, and user experience.</li>
                <li>Communicate updates, newsletters, and promotional materials with your consent.</li>
                <li>Comply with legal and regulatory requirements.</li>
              </ul>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">3. Data Security and Sharing</h2>
              <p className="mb-6 leading-relaxed">
                We implement robust security measures to protect your personal data from unauthorized access, alteration, or disclosure. We do not sell your personal information to third parties. We may share necessary data with trusted service providers who assist us in operating our business, under strict confidentiality agreements.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">4. Cookies and Tracking</h2>
              <p className="mb-6 leading-relaxed">
                Our website uses cookies to enhance user experience, track site usage, and personalize content. You can manage your cookie preferences through your browser settings, though disabling cookies may affect certain functionalities of our site.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">5. Your Rights and Contact Information</h2>
              <p className="mb-6 leading-relaxed">
                You have the right to access, correct, or request the deletion of your personal data held by us. If you have any questions or concerns regarding this Privacy Policy or your data, please contact us at:
              </p>
              <p className="mb-6 font-semibold">Email: info@mgmgroup.in<br />Phone: +91 898 989 8989</p>
            </>
          )}
        </div>
      </div>
    </main>
  )
}
