'use client'

import { useEffect, useState } from 'react'
import { useMarketplacePreferences } from '@/components/MarketplaceShell'
import {
  cloneMarketplaceHomepageContent,
  resolveMarketplaceHomepageSections,
  type MarketplaceHomepageConfig,
  type MarketplaceHomepageSectionType,
} from '@rentaldrivego/types'
import { resolveBrowserAppUrl } from '@/lib/appUrls'

const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:4000/api/v1'

export default function HomeContent() {
  const { language } = useMarketplacePreferences()
  const [content, setContent] = useState<MarketplaceHomepageConfig>(cloneMarketplaceHomepageContent())
  const dict = content[language]
  const sections = resolveMarketplaceHomepageSections(dict.sections)
  const dashboardUrl = resolveBrowserAppUrl(process.env.NEXT_PUBLIC_DASHBOARD_URL ?? 'http://localhost:3000/dashboard')
  const starterSignupHref = `${dashboardUrl}/sign-up?plan=STARTER&billing=MONTHLY&currency=MAD`

  useEffect(() => {
    let cancelled = false

    async function loadHomepageContent() {
      try {
        const res = await fetch(`${API_BASE}/site/platform/homepage`, { cache: 'no-store' })
        const json = await res.json().catch(() => null)
        if (!res.ok || !json?.data || cancelled) return
        setContent(json.data as MarketplaceHomepageConfig)
      } catch {
        // Fall back to the shared defaults when the API is unavailable.
      }
    }

    loadHomepageContent()

    return () => {
      cancelled = true
    }
  }, [])

  function hasSection(section: MarketplaceHomepageSectionType) {
    return sections.includes(section)
  }

  return (
    <main className="min-h-screen overflow-hidden bg-transparent">
      <section className="relative">
        <div className="site-glow absolute inset-x-0 top-0 h-[38rem]" />
        <div className="shell relative py-10 sm:py-14 lg:py-20">
          <div className={`grid gap-8 ${hasSection('hero') && hasSection('surface') ? 'lg:grid-cols-[minmax(0,1.15fr)_24rem] lg:items-stretch xl:grid-cols-[minmax(0,1.2fr)_28rem]' : ''}`}>
            {hasSection('hero') ? (
            <div className="rounded-[2rem] border border-stone-200/80 bg-white/80 p-7 shadow-[0_30px_80px_rgba(28,25,23,0.08)] backdrop-blur dark:border-blue-900 dark:bg-blue-950/70">
              <p className="text-xs font-bold uppercase tracking-[0.32em] text-orange-700 dark:text-orange-300">{dict.heroKicker}</p>
              <h1 className="mt-5 max-w-4xl text-5xl font-black leading-none tracking-[-0.04em] text-blue-950 dark:text-stone-50 sm:text-6xl lg:text-7xl">
                {dict.heroTitle}
              </h1>
              <p className="mt-6 max-w-2xl text-base leading-8 text-stone-600 dark:text-stone-300 sm:text-lg">
                {dict.heroBody}
              </p>

              <div className="mt-8 flex flex-wrap gap-3">
                <a
                  href={starterSignupHref}
                  className="rounded-full bg-orange-600 px-6 py-3 text-sm font-semibold text-white transition hover:bg-orange-700 dark:bg-orange-500 dark:hover:bg-orange-400"
                >
                  {dict.startTrial}
                </a>
                <a
                  href="/explore"
                  className="rounded-full border border-stone-300 bg-white/85 px-6 py-3 text-sm font-semibold text-stone-700 transition hover:border-blue-900 hover:text-blue-900 dark:border-blue-800 dark:bg-blue-950/30 dark:text-stone-200 dark:hover:border-stone-200 dark:hover:text-white"
                >
                  {dict.exploreVehicles}
                </a>
              </div>

              <div className="mt-10 grid gap-3 sm:grid-cols-3">
                {dict.metrics.map(({ value, label }) => (
                  <div key={value} className="rounded-[1.5rem] border border-stone-200/80 bg-stone-50/90 p-4 dark:border-blue-900 dark:bg-blue-950/50">
                    <p className="text-xs font-bold tracking-[0.28em] text-stone-400 dark:text-stone-500">{value}</p>
                    <p className="mt-3 text-sm font-semibold leading-6 text-stone-900 dark:text-stone-100">{label}</p>
                  </div>
                ))}
              </div>
            </div>
            ) : null}

            {hasSection('surface') ? (
            <div className="relative overflow-hidden rounded-[2rem] border border-blue-900/60 bg-[#06132e] p-6 text-white shadow-[0_30px_80px_rgba(6,19,46,0.40)] dark:border-blue-800">
              <div className="absolute inset-0 bg-[radial-gradient(circle_at_top_right,rgba(249,115,22,0.30),transparent_28%),radial-gradient(circle_at_bottom_left,rgba(96,165,250,0.22),transparent_32%)]" />
              <div className="relative">
                <div className="flex items-center justify-between gap-3">
                  <span className="rounded-full border border-white/15 bg-white/10 px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.22em] text-orange-200">
                    {dict.surfaceLabel}
                  </span>
                  <span className="rounded-full bg-emerald-400/15 px-3 py-1 text-[11px] font-semibold uppercase tracking-[0.22em] text-emerald-200">
                    {dict.liveLabel}
                  </span>
                </div>

                <h2 className="mt-6 text-3xl font-black leading-tight tracking-[-0.03em]">{dict.surfaceTitle}</h2>
                <p className="mt-4 text-sm leading-7 text-stone-300">{dict.surfaceBody}</p>

                <div className="mt-8 space-y-3">
                  {[
                    ['01', dict.trustedFleets],
                    ['02', dict.brandedFlows],
                    ['03', dict.multiTenant],
                  ].map(([step, label]) => (
                    <div key={step} className="flex items-center justify-between rounded-[1.25rem] border border-white/10 bg-white/5 px-4 py-4">
                      <div className="flex items-center gap-3">
                        <span className="flex h-10 w-10 items-center justify-center rounded-full bg-white text-xs font-bold text-blue-950">
                          {step}
                        </span>
                        <p className="text-sm font-semibold text-white">{label}</p>
                      </div>
                      <span className="text-lg text-orange-300">+</span>
                    </div>
                  ))}
                </div>
              </div>
            </div>
            ) : null}
          </div>

          {hasSection('audiences') ? <div className="mt-8 grid gap-4 lg:grid-cols-[1.15fr_0.85fr]">
            <article className="rounded-[2rem] border border-stone-200/80 bg-white/80 p-7 backdrop-blur dark:border-blue-900 dark:bg-blue-950/60">
              <p className="text-xs font-bold uppercase tracking-[0.24em] text-stone-500 dark:text-stone-400">{dict.companyKicker}</p>
              <h2 className="mt-4 text-2xl font-black tracking-[-0.03em] text-blue-950 dark:text-stone-50 sm:text-3xl">{dict.companyTitle}</h2>
              <p className="mt-4 max-w-3xl text-sm leading-7 text-stone-600 dark:text-stone-300">{dict.companyBody}</p>
            </article>
            <article className="rounded-[2rem] border border-stone-200/80 bg-[#f7efe2] p-7 dark:border-blue-900 dark:bg-[#1d1712]">
              <p className="text-xs font-bold uppercase tracking-[0.24em] text-orange-700 dark:text-orange-300">{dict.renterKicker}</p>
              <h2 className="mt-4 text-2xl font-black tracking-[-0.03em] text-blue-950 dark:text-stone-50 sm:text-3xl">{dict.renterTitle}</h2>
              <p className="mt-4 text-sm leading-7 text-stone-700 dark:text-stone-300">{dict.renterBody}</p>
            </article>
          </div> : null}
        </div>
      </section>

      {hasSection('pillars') ? <section className="shell pb-10">
        <div className="grid gap-4 lg:grid-cols-3">
          {dict.pillars.map(({ title, body }, index) => (
            <article
              key={title}
              className={`rounded-[2rem] border p-7 shadow-sm ${
                index === 1
                  ? 'border-orange-700 bg-orange-600 text-white dark:border-orange-500 dark:bg-orange-500 dark:text-white'
                  : 'border-stone-200 bg-white dark:border-blue-900 dark:bg-blue-950'
              }`}
            >
              <p
                className={`text-xs font-bold uppercase tracking-[0.26em] ${
                  index === 1 ? 'text-stone-300 dark:text-stone-700' : 'text-stone-400 dark:text-stone-500'
                }`}
              >
                0{index + 1}
              </p>
              <h3 className="mt-4 text-2xl font-black tracking-[-0.03em]">{title}</h3>
              <p
                className={`mt-4 text-sm leading-7 ${
                  index === 1 ? 'text-stone-200 dark:text-stone-800' : 'text-stone-600 dark:text-stone-300'
                }`}
              >
                {body}
              </p>
            </article>
          ))}
        </div>
      </section> : null}

      {(hasSection('features') || hasSection('steps')) ? <section className="shell py-10">
        <div className="grid gap-6 lg:grid-cols-[0.9fr_1.1fr]">
          {hasSection('features') ? (
          <div className="rounded-[2rem] border border-stone-200 bg-[linear-gradient(160deg,#f5f8ff_0%,#edf2ff_100%)] p-8 dark:border-blue-900 dark:bg-[linear-gradient(160deg,#0c1830_0%,#10203e_100%)]">
            <p className="text-xs font-bold uppercase tracking-[0.24em] text-stone-500 dark:text-stone-400">{dict.featureLabel}</p>
            <div className="mt-6 space-y-3">
              {dict.features.map((item, index) => (
                <div key={item} className="flex items-start gap-4 rounded-[1.25rem] border border-stone-200/80 bg-white/85 px-4 py-4 dark:border-blue-800 dark:bg-blue-950/40">
                  <span className="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-orange-600 text-xs font-bold text-white dark:bg-orange-500 dark:text-white">
                    {index + 1}
                  </span>
                  <p className="text-sm font-semibold leading-6 text-stone-800 dark:text-stone-100">{item}</p>
                </div>
              ))}
            </div>
          </div>
          ) : <div />}

          {hasSection('steps') ? (
          <div className="rounded-[2rem] border border-stone-200 bg-white p-8 shadow-sm dark:border-blue-900 dark:bg-blue-950">
            <div className="flex items-end justify-between gap-4">
              <div>
                <p className="text-xs font-bold uppercase tracking-[0.24em] text-orange-700 dark:text-orange-300">{dict.readyKicker}</p>
                <h2 className="mt-4 text-3xl font-black tracking-[-0.03em] text-blue-950 dark:text-stone-50">{dict.stepsTitle}</h2>
              </div>
            </div>

            <div className="mt-8 space-y-4">
              {dict.steps.map(({ step, title, body }) => (
                <article key={step} className="rounded-[1.5rem] border border-stone-200 bg-stone-50 p-5 dark:border-blue-900 dark:bg-blue-950">
                  <p className="text-xs font-bold uppercase tracking-[0.24em] text-stone-500 dark:text-stone-400">
                    {dict.stepLabel} {step}
                  </p>
                  <h3 className="mt-3 text-lg font-black text-blue-950 dark:text-stone-50">{title}</h3>
                  <p className="mt-3 text-sm leading-7 text-stone-600 dark:text-stone-300">{body}</p>
                </article>
              ))}
            </div>
          </div>
          ) : <div />}
        </div>
      </section> : null}

      {hasSection('closing') ? <section className="shell pb-16 pt-6 sm:pb-20">
        <div className="overflow-hidden rounded-[2rem] border border-blue-900/60 bg-[#06132e] px-8 py-10 text-white dark:border-blue-800">
          <div className="grid gap-8 lg:grid-cols-[1fr_auto] lg:items-end">
            <div>
              <p className="text-xs font-bold uppercase tracking-[0.28em] text-orange-300">{dict.readyKicker}</p>
              <h2 className="mt-4 max-w-3xl text-4xl font-black leading-tight tracking-[-0.04em] sm:text-5xl">{dict.readyTitle}</h2>
              <p className="mt-5 max-w-2xl text-sm leading-8 text-stone-300 sm:text-base">{dict.readyBody}</p>
            </div>

            <div className="flex flex-wrap gap-3 lg:justify-end">
              <a
                href="/pricing"
                className="rounded-full border border-white/20 px-6 py-3 text-sm font-semibold text-white transition hover:bg-white hover:text-blue-900"
              >
                {dict.viewPricing}
              </a>
              <a
                href={starterSignupHref}
                className="rounded-full bg-orange-300 px-6 py-3 text-sm font-semibold text-blue-950 transition hover:bg-orange-200"
              >
                {dict.createWorkspace}
              </a>
            </div>
          </div>
        </div>
      </section> : null}
    </main>
  )
}
