'use client';

import React, { useState } from 'react';
import Link from 'next/link';
import { Button, Collapse } from 'antd';
import { Icon, type IconName } from '../ui/Icon';
import { BrandArtwork } from './BrandArtwork';
import { AccessIllustration, CollaborationIllustration, CommandIllustration, DevicesIllustration, InviteIllustration, LayersIllustration, PasswordIllustration, ThemeIllustration, WorkspaceReadyIllustration } from './LandingIllustrations';
import { ParallaxLayer, ScrollReveal, useScrolledPast } from './ScrollReveal';

const NAV_LINKS: Array<[string, string]> = [
  ['#features', 'Features'],
  ['#how-it-works', 'How it works'],
  ['#collaboration', 'Collaboration'],
  ['#security', 'Security'],
  ['#faq', 'FAQ'],
];

const HIGHLIGHTS: Array<{ icon: IconName; title: string; summary: string }> = [
  { icon: 'layers', title: 'Shared spaces', summary: 'Projects, files and discussions in one organised place.' },
  { icon: 'bell', title: 'Timely updates', summary: 'Know what changed without having to ask.' },
  { icon: 'shield', title: 'Role-based access', summary: 'Everyone sees exactly what they should.' },
  { icon: 'globe', title: 'Any device', summary: 'A consistent experience on every screen.' },
];

const FEATURES: Array<{ title: string; summary: string; art: React.ComponentType; wide?: boolean }> = [
  { title: 'A single source of truth', summary: 'Keep plans, files and conversations side by side, so the latest version is always the one in front of you.', art: LayersIllustration, wide: true },
  { title: 'Collaboration in context', summary: 'Comment, hand off and follow progress right where the work happens.', art: CollaborationIllustration },
  { title: 'Access you control', summary: 'Invite people individually and give each role exactly the permissions it needs.', art: AccessIllustration },
  { title: 'Designed for every screen', summary: 'A responsive interface that feels as natural on a phone as it does on a widescreen monitor.', art: DevicesIllustration, wide: true },
  { title: 'Find anything in seconds', summary: 'Quick search take you straight to the page or record you need.', art: CommandIllustration, wide: true },
];

const STEPS: Array<{ title: string; summary: string; art: React.ComponentType }> = [
  { title: 'Accept your invitation', summary: 'Your administrator invites you by email. One click opens a secure, single-use link.', art: InviteIllustration },
  { title: 'Create your password', summary: 'Confirm your details and choose a strong password. Your account is ready straight away.', art: PasswordIllustration },
  { title: 'Join your team', summary: 'Sign in to find your projects, teammates and latest updates already waiting for you.', art: WorkspaceReadyIllustration },
];

const COLLABORATION_POINTS: Array<{ icon: IconName; title: string; summary: string }> = [
  { icon: 'eye', title: 'Visible progress', summary: 'See what’s done, what’s next and who’s on it at a glance.' },
  { icon: 'note', title: 'Context that stays put', summary: 'Discussions remain attached to the work they’re about.' },
  { icon: 'bell', title: 'Updates that matter', summary: 'Hear about the changes relevant to you, and only those.' },
];

const SECURITY_POINTS: Array<{ icon: IconName; title: string; summary: string }> = [
  { icon: 'mail', title: 'Invitation-only membership', summary: 'There is no public sign-up. Administrators decide exactly who joins.' },
  { icon: 'key', title: 'Role-based permissions', summary: 'Control who can view, edit, export or remove information.' },
  { icon: 'building', title: 'Isolated workspaces', summary: 'Each organisation’s information is kept completely separate from every other.' },
  { icon: 'lock', title: 'Protected sign-in', summary: 'Securely hashed passwords, lockout after repeated failures and single-use reset links.' },
];

const FAQS: Array<{ q: string; a: string }> = [
  { q: 'How do I gain access to my organisation’s workspace?', a: 'Workspaces are invitation-only. Your administrator sends an invitation to your work email address. Follow the link to confirm your details, create a password and join your team.' },
  { q: 'How does my team collaborate inside the workspace?', a: 'Team members share a single, organised space for their work. Updates, comments and assigned tasks are visible to the relevant people, so everyone stays informed without separate email threads.' },
  { q: 'How is our organisation’s information protected?', a: 'Each organisation operates in an isolated workspace that only invited members can enter. Permissions are managed by role, passwords are stored using one-way hashing and accounts are locked after repeated failed sign-in attempts.' },
  { q: 'Can I use the workspace on mobile devices?', a: 'Yes. The interface is fully responsive and works in modern browsers on phones, tablets, laptops and desktop computers. No separate installation is required.' },
  { q: 'What should I do if I forget my password?', a: 'Select “Forgot password?” on the sign-in page and enter your work email address. You will receive a secure link to set a new password; each link can be used only once.' },
  { q: 'Who should I contact for help or support?', a: 'For access, roles and permissions, contact your organisation’s workspace administrator. They can resend invitations, update your role and help with any account questions.' },
];

const FOOTER_COLUMNS: Array<{ title: string; links: Array<[string, string]> }> = [
  {
    title: 'Product',
    links: [
      ['#features', 'Features'],
      ['#how-it-works', 'How it works'],
      ['#collaboration', 'Collaboration'],
      ['#security', 'Security'],
    ],
  },
  {
    title: 'Account',
    links: [
      ['/login', 'Sign in'],
      ['/forgot-password', 'Reset password'],
    ],
  },
  {
    title: 'Help',
    links: [
      ['#faq', 'Frequently asked questions'],
      ['#how-it-works', 'Getting started'],
    ],
  },
];

function SectionHeading({ eyebrow, title, summary, align = 'center', onNavy = false }: { eyebrow: string; title: React.ReactNode; summary?: string; align?: 'center' | 'left'; onNavy?: boolean }) {
  return (
    <ScrollReveal direction="up" once className={align === 'center' ? 'mx-auto max-w-2xl text-center' : 'max-w-xl'}>
      <span className={`text-xs font-semibold tracking-[0.16em] uppercase ${onNavy ? 'text-[#ff8a6b]' : 'text-primary'}`}>{eyebrow}</span>
      <h2 className={`mt-4 text-3xl leading-[1.12] font-bold tracking-[-0.03em] sm:text-[44px] ${onNavy ? 'text-white' : 'text-ink'}`}>{title}</h2>
      {summary ? <p className={`mt-5 text-base leading-relaxed sm:text-lg ${onNavy ? 'text-slate-400' : 'text-muted'}`}>{summary}</p> : null}
    </ScrollReveal>
  );
}

function BrandMark({ size = 'md' }: { size?: 'sm' | 'md' }) {
  return <span className={`grid place-items-center bg-gradient-to-br from-[#ff6b4a] to-[#b31800] font-extrabold text-white shadow-[0_8px_20px_-6px_rgba(201,28,0,0.55)] ${size === 'sm' ? 'h-8 w-8 rounded-[10px] text-sm' : 'h-9 w-9 rounded-[11px] text-base'}`}>T</span>;
}

/** Public marketing page shown at "/" for signed-out visitors. */
export function ModernLandingPage() {
  const scrolled = useScrolledPast(12);
  const [menuOpen, setMenuOpen] = useState(false);

  return (
    <div className="min-h-screen overflow-x-clip bg-page text-ink">
      <header className={`fixed inset-x-0 top-0 z-50 transition-all duration-300 ${scrolled ? 'border-b border-line bg-surface/80 backdrop-blur-xl' : 'border-b border-transparent'}`}>
        <div className="mx-auto flex h-[72px] max-w-7xl items-center gap-10 px-4 sm:px-6 lg:px-8">
          <Link href="/" className="flex items-center gap-2.5">
            <BrandMark />
            <span className="text-[15px] font-bold tracking-[-0.01em] text-ink">TechCMantix</span>
          </Link>

          <nav className="hidden items-center gap-8 text-sm lg:flex">
            {NAV_LINKS.map(([href, label]) => (
              <a key={href} href={href} className="text-ink-2 transition-colors hover:text-ink">
                {label}
              </a>
            ))}
          </nav>

          <div className="ml-auto flex items-center gap-2.5">
            <Link href="/login" className="hidden sm:block">
              <Button type="primary" shape="round" size="large" icon={<Icon name="arrowRight" size={15} />} iconPlacement="end">
                Sign in
              </Button>
            </Link>
            <button type="button" onClick={() => setMenuOpen((open) => !open)} aria-label="Toggle navigation menu" aria-expanded={menuOpen} className="grid h-10 w-10 cursor-pointer place-items-center rounded-full border border-line bg-surface text-ink lg:hidden">
              <Icon name={menuOpen ? 'x' : 'menu'} size={17} />
            </button>
          </div>
        </div>

        {menuOpen ? (
          <div className="border-t border-line bg-surface px-4 py-3 lg:hidden">
            <div className="flex flex-col gap-1 text-sm font-medium">
              {NAV_LINKS.map(([href, label]) => (
                <a key={href} href={href} onClick={() => setMenuOpen(false)} className="rounded-lg px-3 py-2.5 text-ink-2 hover:bg-sunken hover:text-ink">
                  {label}
                </a>
              ))}
              <Link href="/login" className="rounded-lg px-3 py-2.5 font-semibold text-primary hover:bg-sunken">
                Sign in
              </Link>
            </div>
          </div>
        ) : null}
      </header>

      <main>
        <section className="tcx-mesh relative overflow-hidden pt-[128px] pb-16 sm:pb-20">
          <div className="relative mx-auto grid max-w-7xl items-center gap-14 px-4 sm:px-6 lg:grid-cols-[1.02fr_0.98fr] lg:px-8">
            <div>
              <div className="tcx-hero-enter tcx-glass inline-flex items-center gap-2 rounded-full py-1.5 pr-4 pl-1.5 text-[13px] font-medium text-ink-2">
                <span className="rounded-full bg-gradient-to-r from-[#ec3f20] to-[#ff8a6b] px-2.5 py-0.5 text-xs font-semibold text-white">Team workspace</span>
                Built for focused, connected teams
              </div>
              <h1 className="tcx-hero-enter mt-7 text-[42px] leading-[1.04] font-extrabold tracking-[-0.045em] text-ink sm:text-6xl lg:text-[68px]" style={{ animationDelay: '80ms' }}>
                Your team’s work, organised in one <span className="tcx-text-gradient">calm workspace</span>.
              </h1>
              <p className="tcx-hero-enter mt-7 max-w-xl text-lg leading-relaxed text-muted" style={{ animationDelay: '160ms' }}>
                Give every teammate a clear view of priorities, shared context for every decision and a secure place to keep it all, on any device.
              </p>
              <div className="tcx-hero-enter mt-10 flex flex-wrap gap-3" style={{ animationDelay: '240ms' }}>
                <Link href="/login">
                  <Button type="primary" shape="round" size="large" icon={<Icon name="arrowRight" size={16} />} iconPlacement="end">
                    Sign in to your workspace
                  </Button>
                </Link>
                <a href="#how-it-works">
                  <Button shape="round" size="large">
                    See how it works
                  </Button>
                </a>
              </div>
              <p className="tcx-hero-enter mt-6 flex items-center gap-2 text-[13px] text-muted" style={{ animationDelay: '320ms' }}>
                <Icon name="mail" size={14} />
                New here? Ask your workspace administrator for an invitation.
              </p>
            </div>

            <div className="tcx-hero-enter" style={{ animationDelay: '200ms' }}>
              <ParallaxLayer speed={-0.05}>
                <BrandArtwork variant="hero" />
              </ParallaxLayer>
            </div>
          </div>

          <div className="relative mx-auto mt-16 grid max-w-7xl gap-4 px-4 sm:grid-cols-2 sm:px-6 lg:mt-20 lg:grid-cols-4 lg:px-8">
            {HIGHLIGHTS.map((item, index) => (
              <ScrollReveal key={item.title} direction="up" once delay={index * 80}>
                <div className="tcx-glass flex h-full items-start gap-3.5 rounded-2xl p-5">
                  <span className="grid h-10 w-10 shrink-0 place-items-center rounded-xl bg-[var(--tcx-primary-soft)] text-primary">
                    <Icon name={item.icon} size={18} />
                  </span>
                  <div>
                    <h3 className="text-[15px] font-semibold text-ink">{item.title}</h3>
                    <p className="mt-1 text-[13.5px] leading-relaxed text-muted">{item.summary}</p>
                  </div>
                </div>
              </ScrollReveal>
            ))}
          </div>
        </section>

        <section id="features" className="scroll-mt-20 py-24 sm:py-28">
          <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
            <SectionHeading eyebrow="Platform" title="Built around the way modern teams work." summary="Organise work, share context and move forward together, with tools that stay simple as your team grows." />

            <div className="mt-16 grid gap-5 md:grid-cols-2 lg:grid-cols-3">
              {FEATURES.map((feature, index) => {
                const Art = feature.art;
                return (
                  <ScrollReveal key={feature.title} direction="up" once delay={(index % 3) * 90} className={feature.wide ? 'lg:col-span-2' : ''}>
                    <article className="group flex h-full flex-col overflow-hidden rounded-[28px] border border-line bg-surface transition-all duration-500 hover:-translate-y-1 hover:shadow-[0_30px_60px_-30px_rgba(2,6,38,0.25)]">
                      <div className="relative h-56 overflow-hidden bg-gradient-to-b from-sunken to-surface px-8 pt-8">
                        <div className="absolute inset-x-10 top-6 h-40 rounded-full bg-[#ff8a6b]/10 blur-3xl" />
                        <div className="relative h-full transition-transform duration-700 group-hover:scale-[1.04]">
                          <Art />
                        </div>
                      </div>
                      <div className="p-8 pt-6">
                        <h3 className="text-xl font-semibold tracking-[-0.02em] text-ink">{feature.title}</h3>
                        <p className="mt-2.5 max-w-md text-[15px] leading-relaxed text-muted">{feature.summary}</p>
                      </div>
                    </article>
                  </ScrollReveal>
                );
              })}
              <ScrollReveal direction="up" once delay={180}>
                <article className="tcx-navy-band flex h-full flex-col justify-between overflow-hidden rounded-[28px] p-8 text-white">
                  <div className="relative h-40">
                    <ThemeIllustration />
                  </div>
                  <div>
                    <h3 className="text-xl font-semibold tracking-[-0.02em]">Comfortable, day or night</h3>
                    <p className="mt-2.5 text-[15px] leading-relaxed text-slate-400">Switch between light and dark themes, or let the workspace follow your device.</p>
                  </div>
                </article>
              </ScrollReveal>
            </div>
          </div>
        </section>

        <section id="how-it-works" className="scroll-mt-20 border-y border-line bg-surface py-24 sm:py-28">
          <div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
            <SectionHeading eyebrow="Getting started" title="From invitation to your first task in minutes." summary="Joining your team’s workspace takes three short steps, with no installation and no setup on your side." />
            <div className="relative mt-16 grid gap-6 md:grid-cols-3">
              <div aria-hidden="true" className="absolute top-[88px] right-[18%] left-[18%] hidden h-px bg-gradient-to-r from-transparent via-[#ff8a6b]/70 to-transparent md:block" />
              {STEPS.map((step, index) => {
                const Art = step.art;
                return (
                  <ScrollReveal key={step.title} direction="up" once delay={index * 120}>
                    <article className="relative flex h-full flex-col rounded-[28px] border border-line bg-page p-6">
                      <div className="relative h-44">
                        <Art />
                      </div>
                      <span className="mt-4 text-xs font-semibold tracking-[0.14em] text-primary uppercase">Step {index + 1}</span>
                      <h3 className="mt-2 text-lg font-semibold tracking-[-0.01em] text-ink">{step.title}</h3>
                      <p className="mt-2 text-[15px] leading-relaxed text-muted">{step.summary}</p>
                    </article>
                  </ScrollReveal>
                );
              })}
            </div>
          </div>
        </section>

        <section id="collaboration" className="tcx-navy-band tcx-on-navy relative scroll-mt-20 overflow-hidden py-24 sm:py-28">
          <div className="tcx-grid-lines pointer-events-none absolute inset-0" />
          <div className="relative mx-auto grid max-w-7xl items-center gap-16 px-4 sm:px-6 lg:grid-cols-2 lg:px-8">
            <div>
              <SectionHeading
                eyebrow="Collaboration"
                align="left"
                onNavy
                title={
                  <>
                    Stay aligned without the <span className="tcx-text-gradient">extra meetings</span>.
                  </>
                }
                summary="Updates, comments and progress reach the people who need them the moment they happen, so your team spends less time chasing status and more time doing the work."
              />
              <div className="mt-12 grid gap-6 sm:grid-cols-3">
                {COLLABORATION_POINTS.map((item, index) => (
                  <ScrollReveal key={item.title} direction="up" once delay={index * 100}>
                    <div className="border-t border-white/10 pt-5">
                      <Icon name={item.icon} size={20} className="text-[#ff8a6b]" />
                      <h3 className="mt-3 text-base font-semibold text-white">{item.title}</h3>
                      <p className="mt-1.5 text-sm leading-relaxed text-slate-400">{item.summary}</p>
                    </div>
                  </ScrollReveal>
                ))}
              </div>
            </div>
            <ScrollReveal direction="scale" once>
              <ParallaxLayer speed={-0.06} maxShift={50}>
                <BrandArtwork variant="auth" />
              </ParallaxLayer>
            </ScrollReveal>
          </div>
        </section>

        <section id="security" className="scroll-mt-20 py-24 sm:py-28">
          <div className="mx-auto grid max-w-7xl items-center gap-16 px-4 sm:px-6 lg:grid-cols-[0.9fr_1.1fr] lg:px-8">
            <ScrollReveal direction="left" once>
              <div className="relative mx-auto aspect-[1.1/1] w-full max-w-[480px] overflow-hidden rounded-[32px] border border-line bg-gradient-to-b from-sunken to-surface p-10">
                <div className="tcx-glow absolute inset-[12%] rounded-[40%]" />
                <div className="relative grid h-full place-items-center">
                  <div className="h-56 w-full scale-[1.3]">
                    <AccessIllustration />
                  </div>
                </div>
              </div>
            </ScrollReveal>
            <div>
              <SectionHeading eyebrow="Security and privacy" align="left" title="Your workspace stays yours." summary="Security is part of the foundation, not an add-on. Every layer is designed so the right people have access, and nobody else does." />
              <div className="mt-12 grid gap-x-10 gap-y-8 sm:grid-cols-2">
                {SECURITY_POINTS.map((point, index) => (
                  <ScrollReveal key={point.title} direction="up" once delay={index * 80}>
                    <div className="flex gap-4">
                      <span className="grid h-11 w-11 shrink-0 place-items-center rounded-2xl bg-[var(--tcx-primary-soft)] text-primary">
                        <Icon name={point.icon} size={19} />
                      </span>
                      <div>
                        <h3 className="text-base font-semibold text-ink">{point.title}</h3>
                        <p className="mt-1 text-sm leading-relaxed text-muted">{point.summary}</p>
                      </div>
                    </div>
                  </ScrollReveal>
                ))}
              </div>
            </div>
          </div>
        </section>

        <section id="faq" className="scroll-mt-20 border-t border-line bg-surface py-24 sm:py-28">
          <div className="mx-auto grid max-w-7xl gap-12 px-4 sm:px-6 lg:grid-cols-[0.8fr_1.2fr] lg:gap-16 lg:px-8">
            <div className="flex flex-col gap-10">
              <SectionHeading eyebrow="FAQ" align="left" title="Frequently asked questions" summary="Answers to common questions about access, collaboration and keeping your account secure." />
              <ScrollReveal direction="up" once delay={120}>
                <div className="rounded-[24px] border border-line bg-page p-6">
                  <span className="grid h-11 w-11 place-items-center rounded-2xl bg-[var(--tcx-primary-soft)] text-primary">
                    <Icon name="helpCircle" size={20} />
                  </span>
                  <h3 className="mt-4 text-lg font-semibold text-ink">Still need help?</h3>
                  <p className="mt-1.5 text-[15px] leading-relaxed text-muted">Your workspace administrator can resend invitations, update your role and answer account questions.</p>
                  <div className="mt-5 flex flex-wrap gap-x-6 gap-y-2 text-sm font-semibold">
                    <Link href="/forgot-password" className="inline-flex items-center gap-1.5 text-primary">
                      Reset your password
                      <Icon name="arrowRight" size={14} />
                    </Link>
                    <Link href="/login" className="inline-flex items-center gap-1.5 text-primary">
                      Go to sign in
                      <Icon name="arrowRight" size={14} />
                    </Link>
                  </div>
                </div>
              </ScrollReveal>
            </div>
            <ScrollReveal direction="up" once delay={100}>
              <Collapse accordion ghost size="large" expandIconPlacement="end" defaultActiveKey={['0']} items={FAQS.map((item, index) => ({ key: String(index), label: <span className="text-base font-semibold text-ink">{item.q}</span>, children: <p className="m-0 max-w-2xl text-[15px] leading-relaxed text-muted">{item.a}</p> }))} />
            </ScrollReveal>
          </div>
        </section>

        <section className="px-4 py-24 sm:px-6 lg:px-8">
          <ScrollReveal direction="scale" once>
            <div className="tcx-navy-band tcx-on-navy relative mx-auto max-w-7xl overflow-hidden rounded-[36px] px-8 py-16 sm:px-16 sm:py-20">
              <div className="tcx-grid-lines pointer-events-none absolute inset-0" />
              <div className="absolute top-1/2 right-[2%] hidden h-[300px] w-[460px] -translate-y-1/2 lg:block">
                <LayersIllustration />
              </div>
              <div className="relative max-w-xl">
                <h2 className="text-3xl leading-tight font-bold tracking-[-0.03em] text-white sm:text-5xl">Ready to get back to work?</h2>
                <p className="mt-5 text-lg leading-relaxed text-slate-400">Sign in to see your team’s latest updates, or reset your password if you’re having trouble getting in.</p>
                <div className="mt-9 flex flex-wrap items-center gap-5">
                  <Link href="/login">
                    <Button type="primary" shape="round" size="large" icon={<Icon name="arrowRight" size={16} />} iconPlacement="end">
                      Sign in to your workspace
                    </Button>
                  </Link>
                </div>
              </div>
            </div>
          </ScrollReveal>
        </section>
      </main>

      <footer className="border-t border-line bg-surface">
        <div className="mx-auto grid max-w-7xl gap-12 px-4 py-16 sm:px-6 md:grid-cols-[1.4fr_repeat(3,1fr)] lg:px-8">
          <div className="max-w-xs">
            <Link href="/" className="flex items-center gap-2.5">
              <BrandMark size="sm" />
              <span className="text-[15px] font-bold text-ink">TechCMantix</span>
            </Link>
            <p className="mt-4 text-sm leading-relaxed text-muted">A secure, focused workspace that keeps your team organised, aligned and moving forward.</p>
            <div className="mt-5 flex flex-wrap gap-2 text-xs font-medium text-ink-2">
              {['Invitation-only', 'Light and dark themes', 'Any device'].map((badge) => (
                <span key={badge} className="rounded-full border border-line px-3 py-1">
                  {badge}
                </span>
              ))}
            </div>
          </div>
          {FOOTER_COLUMNS.map((column) => (
            <div key={column.title}>
              <h4 className="text-xs font-semibold tracking-[0.14em] text-ink uppercase">{column.title}</h4>
              <ul className="mt-4 space-y-3 text-sm">
                {column.links.map(([href, label]) => (
                  <li key={label}>
                    {href.startsWith('/') ? (
                      <Link href={href} className="text-muted transition-colors hover:text-ink">
                        {label}
                      </Link>
                    ) : (
                      <a href={href} className="text-muted transition-colors hover:text-ink">
                        {label}
                      </a>
                    )}
                  </li>
                ))}
              </ul>
            </div>
          ))}
        </div>
        <div className="border-t border-line">
          <div className="mx-auto flex max-w-7xl flex-col gap-4 px-4 py-6 text-[13px] text-muted sm:flex-row sm:items-center sm:justify-between sm:px-6 lg:px-8">
            <span>© 2026 TechCMantix. All rights reserved.</span>
            <div className="flex items-center gap-5">
              <button type="button" onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })} className="inline-flex cursor-pointer items-center gap-1.5 text-muted transition-colors hover:text-ink">
                Back to top
                <Icon name="chevronDown" size={14} className="rotate-180" />
              </button>
            </div>
          </div>
        </div>
      </footer>
    </div>
  );
}
