import { Form, Head, usePage } from "@inertiajs/react";
import { CircleCheck, Send, TriangleAlert } from "lucide-react";
import type { ReactNode } from "react";
import AccountDeletionRequestController from "@/actions/App/Http/Controllers/AccountDeletionRequestController";
import PublicPageHeader from "@/components/landing/public-page-header";
import Reveal from "@/components/landing/reveal";
import { RequiredMark } from "@/components/required-mark";
import { cn } from "@/lib/utils";

const fieldClasses =
    "w-full rounded-landing border border-surface-200 bg-white px-3.5 py-2.5 text-[15px] text-ink shadow-landing-sm transition-[border-color,box-shadow] outline-none placeholder:text-ink-300 focus:border-brand focus:ring-3 focus:ring-brand/15 aria-invalid:border-brand";

const inAppSteps = [
    "Open the mobile app and sign in.",
    "Go to the Account tab and tap Edit Profile.",
    "Scroll down and tap Delete my account.",
    "Enter your password to confirm. Your account is deleted straight away and you are signed out.",
] as const;

function Field({
    id,
    label,
    optional = false,
    error,
    children,
}: {
    id: string;
    label: string;
    optional?: boolean;
    error?: string;
    children: ReactNode;
}) {
    return (
        <div className="flex flex-col gap-1.5">
            <label htmlFor={id} className="text-sm font-semibold text-ink-700">
                {label}
                {!optional && <RequiredMark />}
                {optional && (
                    <span className="ml-1.5 font-normal text-ink-400">
                        (optional)
                    </span>
                )}
            </label>
            {children}
            {error && (
                <p id={`${id}-error`} className="text-sm text-brand">
                    {error}
                </p>
            )}
        </div>
    );
}

function Section({ title, children }: { title: string; children: ReactNode }) {
    return (
        <div>
            <h2 className="mb-3 font-display text-xl font-semibold text-ink">
                {title}
            </h2>
            <div className="text-[15px] leading-[1.75] text-ink-500">
                {children}
            </div>
        </div>
    );
}

export default function DeleteAccount() {
    const page = usePage();
    const { name } = page.props;
    const reference = page.flash.deletionReference;

    return (
        <>
            <Head title="Delete your account" />

            <PublicPageHeader
                kicker="Account"
                title="Delete your account."
                intro={`How to delete your ${name} account and personal data, from the mobile app or by sending us a request.`}
            />

            <section className="px-5 py-14 sm:px-6 lg:px-10 lg:py-20">
                <div className="mx-auto grid w-full max-w-6xl items-start gap-10 lg:grid-cols-[1fr_400px] lg:gap-14">
                    <Reveal className="flex flex-col gap-10">
                        <Section title="Delete it in the app">
                            <ol className="flex flex-col gap-4">
                                {inAppSteps.map((step, index) => (
                                    <li key={step} className="flex gap-4">
                                        <span
                                            aria-hidden="true"
                                            className="flex size-7 shrink-0 items-center justify-center rounded-full bg-brand-faint font-display text-sm font-semibold text-brand"
                                        >
                                            {index + 1}
                                        </span>
                                        <span>{step}</span>
                                    </li>
                                ))}
                            </ol>
                        </Section>

                        <Section title="Can’t sign in? Send us a request">
                            <p>
                                Fill in the form on this page with the email
                                address on your account. We confirm the request
                                with you by email and complete it within 30
                                days.
                            </p>
                        </Section>

                        <Section title="What gets deleted">
                            <ul className="list-disc space-y-1.5 pl-5">
                                <li>
                                    Your profile, sign-in details, photos and
                                    next of kin.
                                </li>
                                <li>
                                    Your access to the app and website — you are
                                    signed out on every device.
                                </li>
                            </ul>
                        </Section>

                        <Section title="What we have to keep">
                            <p>
                                As a regulated savings and loan cooperative, we
                                must keep records of transactions, savings,
                                loans and identity checks (KYC) for as long as
                                the law requires — usually up to 6 years. These
                                records are kept securely, used only to meet
                                legal and regulatory duties, and then deleted.
                            </p>
                            <p className="mt-3">
                                Please withdraw any wallet or savings balance
                                and settle any open loan before deleting your
                                account. An outstanding loan must still be
                                repaid after the account is closed.
                            </p>
                        </Section>
                    </Reveal>

                    <Reveal delay={120} className="lg:sticky lg:top-28">
                        <div className="rounded-landing-xl border border-surface-200 bg-white p-6 shadow-landing sm:p-8">
                            <h2 className="font-display text-2xl font-semibold tracking-[-0.015em] text-ink">
                                Request deletion
                            </h2>
                            <p className="mt-1.5 text-sm leading-relaxed text-ink-500">
                                Fields marked{" "}
                                <span className="text-red-600">*</span> are
                                required.
                            </p>

                            {reference && (
                                <div
                                    role="status"
                                    className="mt-6 flex items-start gap-3 rounded-landing border border-highlight/30 bg-highlight-faint px-4 py-3.5 text-sm text-ink-700"
                                >
                                    <CircleCheck
                                        className="mt-0.5 size-4 shrink-0 text-highlight"
                                        aria-hidden="true"
                                    />
                                    <p>
                                        <strong className="font-bold text-ink">
                                            Request received.
                                        </strong>{" "}
                                        Your reference number is{" "}
                                        <strong className="font-mono font-bold text-ink">
                                            {reference}
                                        </strong>
                                        . We will email you to confirm.
                                    </p>
                                </div>
                            )}

                            <Form
                                {...AccountDeletionRequestController.store.form()}
                                options={{ preserveScroll: true }}
                                resetOnSuccess
                                disableWhileProcessing
                                className="mt-6 flex flex-col gap-5"
                            >
                                {({ processing, errors }) => (
                                    <>
                                        {errors.form && (
                                            <div
                                                role="alert"
                                                className="flex items-start gap-3 rounded-landing border border-brand-border bg-brand-faint px-4 py-3 text-sm text-brand-dark"
                                            >
                                                <TriangleAlert
                                                    className="mt-0.5 size-4 shrink-0"
                                                    aria-hidden="true"
                                                />
                                                <p>{errors.form}</p>
                                            </div>
                                        )}

                                        <Field
                                            id="name"
                                            label="Full name"
                                            error={errors.name}
                                        >
                                            <input
                                                id="name"
                                                name="name"
                                                type="text"
                                                required
                                                autoComplete="name"
                                                aria-invalid={Boolean(
                                                    errors.name,
                                                )}
                                                className={fieldClasses}
                                            />
                                        </Field>
                                        <Field
                                            id="email"
                                            label="Email on your account"
                                            error={errors.email}
                                        >
                                            <input
                                                id="email"
                                                name="email"
                                                type="email"
                                                required
                                                autoComplete="email"
                                                aria-invalid={Boolean(
                                                    errors.email,
                                                )}
                                                className={fieldClasses}
                                            />
                                        </Field>
                                        <Field
                                            id="phone"
                                            label="Phone number"
                                            optional
                                            error={errors.phone}
                                        >
                                            <input
                                                id="phone"
                                                name="phone"
                                                type="tel"
                                                autoComplete="tel"
                                                aria-invalid={Boolean(
                                                    errors.phone,
                                                )}
                                                className={fieldClasses}
                                            />
                                        </Field>
                                        <Field
                                            id="member_id"
                                            label="Member ID"
                                            optional
                                            error={errors.member_id}
                                        >
                                            <input
                                                id="member_id"
                                                name="member_id"
                                                type="text"
                                                aria-invalid={Boolean(
                                                    errors.member_id,
                                                )}
                                                className={fieldClasses}
                                            />
                                        </Field>
                                        <Field
                                            id="reason"
                                            label="Reason"
                                            optional
                                            error={errors.reason}
                                        >
                                            <textarea
                                                id="reason"
                                                name="reason"
                                                rows={3}
                                                maxLength={2000}
                                                aria-invalid={Boolean(
                                                    errors.reason,
                                                )}
                                                className={cn(
                                                    fieldClasses,
                                                    "resize-y",
                                                )}
                                            />
                                        </Field>

                                        <div className="flex flex-col gap-1.5">
                                            <label className="flex items-start gap-2.5 text-sm text-ink-700">
                                                <input
                                                    type="checkbox"
                                                    name="confirm"
                                                    value="1"
                                                    required
                                                    className="mt-0.5 size-4 accent-brand"
                                                />
                                                <span>
                                                    I want my account and
                                                    personal data deleted.
                                                    <RequiredMark />
                                                </span>
                                            </label>
                                            {errors.confirm && (
                                                <p className="text-sm text-brand">
                                                    {errors.confirm}
                                                </p>
                                            )}
                                        </div>

                                        <button
                                            type="submit"
                                            disabled={processing}
                                            className="inline-flex items-center justify-center gap-2 self-start rounded-full bg-brand px-6 py-3 text-sm font-semibold text-white shadow-landing-sm transition-colors hover:bg-brand-hover focus-visible:ring-2 focus-visible:ring-brand focus-visible:ring-offset-2 focus-visible:outline-none disabled:opacity-60"
                                        >
                                            <Send
                                                className="size-4"
                                                aria-hidden="true"
                                            />
                                            {processing
                                                ? "Sending…"
                                                : "Send request"}
                                        </button>
                                    </>
                                )}
                            </Form>
                        </div>
                    </Reveal>
                </div>
            </section>
        </>
    );
}
