import { FormEvent, useState } from "react";
import { Head, Link, useForm } from "@inertiajs/react";
import {
    ArrowRight,
    Building2,
    Eye,
    EyeOff,
    Mail,
    MapPin,
    Phone,
    ShieldCheck,
    UserRound,
} from "lucide-react";
import AppLogo from "@/components/app-logo";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";

export default function RegisterCompany() {
    const [showPassword, setShowPassword] = useState(false);
    const { data, setData, post, processing, errors } = useForm({
        company_name: "",
        company_email: "",
        company_phone: "",
        company_address: "",
        name: "",
        email: "",
        password: "",
        password_confirmation: "",
    });

    const submit = (event: FormEvent) => {
        event.preventDefault();
        post("/register-company");
    };

    return (
        <>
            <Head title="Daftar Perusahaan" />
            <div className="min-h-svh bg-slate-50/70 px-4 py-8 sm:px-6 lg:px-10 flex flex-col justify-center">
                <div className="mx-auto w-full max-w-6xl">
                    <div className="mb-8 flex items-center justify-between">
                        <Link href="/" className="inline-flex items-center">
                            <AppLogo />
                        </Link>
                        <Link
                            href="/login"
                            className="text-sm font-medium text-slate-500 hover:text-slate-900 transition-colors"
                        >
                            Sudah punya akun?{" "}
                            <span className="text-primary font-semibold hover:underline">
                                Login
                            </span>
                        </Link>
                    </div>

                    <div className="overflow-hidden rounded-3xl border border-slate-200/80 bg-white shadow-[0_30px_100px_-30px_rgba(15,23,42,0.12)] lg:grid lg:grid-cols-[1fr_1.65fr]">
                        {/* Kolom Kiri: Branding & Benefit */}
                        <div className="relative overflow-hidden bg-slate-900 p-8 sm:p-12 text-white flex flex-col justify-between">
                            <div className="absolute -right-24 -top-24 size-80 rounded-full bg-primary/20 blur-3xl pointer-events-none" />
                            <div className="absolute -left-24 -bottom-24 size-80 rounded-full bg-blue-600/10 blur-3xl pointer-events-none" />

                            <div className="relative z-10">
                                <div className="inline-flex size-12 items-center justify-center rounded-2xl bg-white/10 ring-1 ring-white/25 shadow-inner">
                                    <Building2 className="size-6 text-primary-foreground" />
                                </div>
                                <h1 className="mt-8 text-3xl sm:text-4xl font-extrabold tracking-tight leading-tight">
                                    Bangun workspace CRM perusahaan Anda.
                                </h1>
                                <p className="mt-4 text-sm sm:text-base leading-relaxed text-slate-300">
                                    Daftarkan perusahaan sekali, lalu kelola
                                    tim, klien, dan seluruh aktivitas bisnis
                                    dari satu platform terpusat yang
                                    profesional.
                                </p>
                            </div>

                            <div className="relative z-10 mt-12 space-y-6">
                                <Benefit
                                    icon={UsersIcon}
                                    title="Manajemen Tim Terstruktur"
                                    text="Atur user, sales, team, role, dan hak akses permissions secara presisi."
                                />
                                <Benefit
                                    icon={MapPin}
                                    title="Pantau Aktivitas Lapangan"
                                    text="Kelola customer, visit, order, invoice, hingga pengiriman dalam satu tempat."
                                />
                                <Benefit
                                    icon={ShieldCheck}
                                    title="Multi-Company Isolation"
                                    text="Keamanan data setiap perusahaan terisolasi secara ketat dan profesional."
                                />
                            </div>

                            <div className="relative z-10 mt-12 pt-6 border-t border-slate-800 text-xs text-slate-400">
                                Platform CRM Enterprise v2.4 • Secure
                                Multi-Tenant
                            </div>
                        </div>

                        {/* Kolom Kanan: Formulir Registrasi (Dibuat Lebih Luas & Nyaman) */}
                        <div className="p-8 sm:p-12 lg:p-14 bg-white">
                            <div className="mb-8">
                                <h2 className="text-2xl sm:text-3xl font-bold tracking-tight text-slate-900">
                                    Buat akun perusahaan
                                </h2>
                                <p className="mt-2 text-sm text-slate-500">
                                    Lengkapi informasi perusahaan dan data
                                    administrator untuk memulai workspace baru
                                    Anda.
                                </p>
                            </div>

                            <form onSubmit={submit} className="space-y-8">
                                <Section title="Informasi Perusahaan">
                                    <div className="grid gap-5 sm:grid-cols-2">
                                        <Field
                                            label="Nama Perusahaan *"
                                            error={errors.company_name}
                                            icon={Building2}
                                            className="sm:col-span-2"
                                        >
                                            <Input
                                                value={data.company_name}
                                                onChange={(e) =>
                                                    setData(
                                                        "company_name",
                                                        e.target.value,
                                                    )
                                                }
                                                required
                                                className="h-11 shadow-xs"
                                                placeholder="PT Nama Perusahaan Indonesia"
                                            />
                                        </Field>
                                        <Field
                                            label="Email Perusahaan"
                                            error={errors.company_email}
                                            icon={Mail}
                                        >
                                            <Input
                                                type="email"
                                                value={data.company_email}
                                                onChange={(e) =>
                                                    setData(
                                                        "company_email",
                                                        e.target.value,
                                                    )
                                                }
                                                className="h-11 shadow-xs"
                                                placeholder="info@perusahaan.com"
                                            />
                                        </Field>
                                        <Field
                                            label="Nomor Telepon"
                                            error={errors.company_phone}
                                            icon={Phone}
                                        >
                                            <Input
                                                value={data.company_phone}
                                                onChange={(e) =>
                                                    setData(
                                                        "company_phone",
                                                        e.target.value,
                                                    )
                                                }
                                                className="h-11 shadow-xs"
                                                placeholder="0812xxxxxxxx"
                                            />
                                        </Field>
                                        <Field
                                            label="Alamat Lengkap Perusahaan"
                                            error={errors.company_address}
                                            icon={MapPin}
                                            className="sm:col-span-2"
                                        >
                                            <Textarea
                                                value={data.company_address}
                                                onChange={(e) =>
                                                    setData(
                                                        "company_address",
                                                        e.target.value,
                                                    )
                                                }
                                                placeholder="Masukkan alamat operasional lengkap perusahaan..."
                                                className="min-h-28 shadow-xs resize-none"
                                            />
                                        </Field>
                                    </div>
                                </Section>

                                <Section title="Akun Administrator Utama">
                                    <div className="grid gap-5 sm:grid-cols-2">
                                        <Field
                                            label="Nama Administrator *"
                                            error={errors.name}
                                            icon={UserRound}
                                            className="sm:col-span-2"
                                        >
                                            <Input
                                                value={data.name}
                                                onChange={(e) =>
                                                    setData(
                                                        "name",
                                                        e.target.value,
                                                    )
                                                }
                                                required
                                                className="h-11 shadow-xs"
                                                placeholder="Nama lengkap pengelola utama"
                                            />
                                        </Field>
                                        <Field
                                            label="Email Login *"
                                            error={errors.email}
                                            icon={Mail}
                                            className="sm:col-span-2"
                                        >
                                            <Input
                                                type="email"
                                                value={data.email}
                                                onChange={(e) =>
                                                    setData(
                                                        "email",
                                                        e.target.value,
                                                    )
                                                }
                                                required
                                                autoComplete="email"
                                                className="h-11 shadow-xs"
                                                placeholder="admin@perusahaan.com"
                                            />
                                        </Field>
                                        <Field
                                            label="Password *"
                                            error={errors.password}
                                            icon={ShieldCheck}
                                        >
                                            <div className="relative">
                                                <Input
                                                    type={
                                                        showPassword
                                                            ? "text"
                                                            : "password"
                                                    }
                                                    value={data.password}
                                                    onChange={(e) =>
                                                        setData(
                                                            "password",
                                                            e.target.value,
                                                        )
                                                    }
                                                    required
                                                    autoComplete="new-password"
                                                    placeholder="Minimal 8 karakter"
                                                    className="h-11 pr-10 shadow-xs"
                                                />
                                                <button
                                                    type="button"
                                                    onClick={() =>
                                                        setShowPassword(
                                                            (v) => !v,
                                                        )
                                                    }
                                                    className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-700 transition-colors"
                                                    aria-label={
                                                        showPassword
                                                            ? "Sembunyikan password"
                                                            : "Tampilkan password"
                                                    }
                                                >
                                                    {showPassword ? (
                                                        <EyeOff className="size-4" />
                                                    ) : (
                                                        <Eye className="size-4" />
                                                    )}
                                                </button>
                                            </div>
                                        </Field>
                                        <Field
                                            label="Konfirmasi Password *"
                                            error={errors.password_confirmation}
                                        >
                                            <Input
                                                type={
                                                    showPassword
                                                        ? "text"
                                                        : "password"
                                                }
                                                value={
                                                    data.password_confirmation
                                                }
                                                onChange={(e) =>
                                                    setData(
                                                        "password_confirmation",
                                                        e.target.value,
                                                    )
                                                }
                                                required
                                                autoComplete="new-password"
                                                placeholder="Ulangi password"
                                                className="h-11 shadow-xs"
                                            />
                                        </Field>
                                    </div>
                                </Section>

                                <div className="rounded-2xl border border-blue-100 bg-blue-50/60 p-4 sm:p-5">
                                    <div className="flex gap-3.5 items-start">
                                        <div className="p-2 rounded-xl bg-primary/10 text-primary shrink-0 mt-0.5">
                                            <ShieldCheck className="size-5" />
                                        </div>
                                        <div>
                                            <p className="text-sm font-semibold text-slate-900">
                                                Trial Eksklusif 14 Hari Gratis
                                            </p>
                                            <p className="mt-1 text-xs sm:text-sm leading-relaxed text-slate-600">
                                                Workspace perusahaan Anda akan
                                                langsung diinisialisasi
                                                otomatis, dan Anda bisa langsung
                                                masuk ke panel CRM setelah
                                                pendaftaran berhasil tanpa kartu
                                                kredit.
                                            </p>
                                        </div>
                                    </div>
                                </div>

                                <Button
                                    type="submit"
                                    disabled={processing}
                                    className="h-12 w-full rounded-xl bg-primary font-semibold text-base shadow-lg shadow-primary/25 hover:bg-primary/90 transition-all gap-2"
                                >
                                    {processing
                                        ? "Memproses pembuatan workspace..."
                                        : "Buat akun perusahaan"}
                                    {!processing && (
                                        <ArrowRight className="size-4" />
                                    )}
                                </Button>
                            </form>
                        </div>
                    </div>
                    <p className="mt-6 text-center text-xs text-slate-400">
                        Dengan mendaftar, Anda menyetujui syarat, ketentuan,
                        serta kebijakan privasi penggunaan platform kami.
                    </p>
                </div>
            </div>
        </>
    );
}

function Section({
    title,
    children,
}: {
    title: string;
    children: React.ReactNode;
}) {
    return (
        <section className="space-y-4 pt-2">
            <div className="border-b border-slate-100 pb-2">
                <h3 className="text-sm font-bold text-slate-900 uppercase tracking-wider">
                    {title}
                </h3>
            </div>
            {children}
        </section>
    );
}

function Field({
    label,
    error,
    icon: Icon,
    className = "",
    children,
}: {
    label: string;
    error?: string;
    icon?: React.ComponentType<{ className?: string }>;
    className?: string;
    children: React.ReactNode;
}) {
    return (
        <div className={`space-y-1.5 ${className}`}>
            <Label className="text-xs font-semibold text-slate-700">
                {label}
            </Label>
            <div className="relative">
                {Icon && (
                    <Icon className="pointer-events-none absolute left-3.5 top-1/2 z-10 size-4 -translate-y-1/2 text-slate-400" />
                )}
                <div
                    className={Icon ? "[&>input]:pl-10 [&>textarea]:pl-10" : ""}
                >
                    {children}
                </div>
            </div>
            {error && (
                <p className="text-xs text-destructive font-medium mt-1">
                    {error}
                </p>
            )}
        </div>
    );
}

function Benefit({
    icon: Icon,
    title,
    text,
}: {
    icon: React.ComponentType<{ className?: string }>;
    title: string;
    text: string;
}) {
    return (
        <div className="flex gap-4 items-start">
            <div className="rounded-xl bg-white/10 p-2.5 ring-1 ring-white/15 shrink-0">
                <Icon className="size-5 text-primary-foreground" />
            </div>
            <div>
                <p className="text-sm font-semibold text-white">{title}</p>
                <p className="mt-1 text-xs sm:text-sm leading-relaxed text-slate-300">
                    {text}
                </p>
            </div>
        </div>
    );
}

function UsersIcon({ className }: { className?: string }) {
    return (
        <svg
            className={className}
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            strokeWidth="1.8"
        >
            <path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2" />
            <circle cx="9" cy="7" r="4" />
            <path d="M22 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75" />
        </svg>
    );
}
