import { Metadata } from "next"
import { getTermsAndConditionsData } from "@/lib/api/terms-and-conditions"

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

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

export const metadata: Metadata = {
  title: "Terms & Conditions | MGM Group",
  description: "Terms and Conditions 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 TermsAndConditionsPage() {
  const data = await getTermsAndConditionsData()

  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">
                Welcome to the MGM Group website. By accessing and using this website, you agree to comply with and be bound by the following Terms and Conditions. Please review them carefully.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">1. Acceptance of Terms</h2>
              <p className="mb-6 leading-relaxed">
                By using our website and services, you signify your acceptance of these Terms. If you do not agree to these Terms, please refrain from using our site. We reserve the right to modify these Terms at any time without prior notice.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">2. Intellectual Property</h2>
              <p className="mb-6 leading-relaxed">
                All content, logos, text, graphics, images, and software on this website are the intellectual property of MGM Group or its affiliates and are protected by applicable copyright and trademark laws. Unauthorized use, reproduction, or distribution of this material is strictly prohibited.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">3. Use of the Website</h2>
              <p className="mb-6 leading-relaxed">You agree to use this website only for lawful purposes. You are prohibited from:</p>
              <ul className="list-disc pl-6 mb-6 space-y-2">
                <li>Attempting to gain unauthorized access to our systems or networks.</li>
                <li>Transmitting any harmful, malicious, or disruptive code.</li>
                <li>Engaging in any activity that could damage, disable, or impair the website&apos;s functionality.</li>
              </ul>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">4. Third-Party Links</h2>
              <p className="mb-6 leading-relaxed">
                Our website may contain links to third-party websites for your convenience. We do not endorse, control, or take responsibility for the content, privacy policies, or practices of any third-party sites. Accessing these links is at your own risk.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">5. Limitation of Liability</h2>
              <p className="mb-6 leading-relaxed">
                To the maximum extent permitted by law, MGM Group and its affiliates shall not be liable for any direct, indirect, incidental, consequential, or punitive damages arising out of your access to or use of this website. The content is provided on an &quot;as-is&quot; basis without warranties of any kind.
              </p>
              <h2 className="text-2xl font-host-grotesk font-bold text-slate-800 mt-10 mb-4">6. Governing Law</h2>
              <p className="mb-6 leading-relaxed">
                These Terms and Conditions shall be governed by and construed in accordance with the laws of India. Any disputes arising from or in connection with these terms shall be subject to the exclusive jurisdiction of the courts located in Chennai, Tamil Nadu.
              </p>
            </>
          )}
        </div>
      </div>
    </main>
  )
}
