import { Form, Head, usePage } from '@inertiajs/react';
import InputError from '@/components/input-error';
import PasswordInput from '@/components/password-input';
import TextLink from '@/components/text-link';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { login } from '@/routes';
import { store } from '@/routes/register';
import {
    formatPackagePrice,
    packageCreditsLine,
    packagePriceUnit,
    type PricingPackage,
} from '@/components/marketing/package-card';

type Props = {
    passwordRules: string;
    /**
     * Il piano scelto in vetrina, risolto dal server a partire da `?package=`.
     * È `null` quando la query non c'è o non corrisponde a un pacchetto
     * attivo: in quel caso la registrazione procede uguale, senza banner.
     */
    chosenPackage?: PricingPackage | null;
};

export default function Register({ passwordRules, chosenPackage }: Props) {
    const { url } = usePage();
    const packageSlug = new URLSearchParams(url.split('?')[1] ?? '').get(
        'package',
    );

    return (
        <>
            <Head title="Crea il tuo account EasyForma" />
            <Form
                {...store.form()}
                resetOnSuccess={['password', 'password_confirmation']}
                disableWhileProcessing
                className="flex flex-col gap-6"
            >
                {({ processing, errors }) => (
                    <>
                        <div className="grid gap-6">
                            {packageSlug && (
                                <>
                                    {chosenPackage ? (
                                        <div className="rounded-marketing bg-primary/8 -mb-2 p-4">
                                            <p className="text-muted-foreground text-sm">
                                                Piano scelto
                                            </p>
                                            <p className="font-display mt-1 text-[1.0625rem] font-semibold">
                                                {chosenPackage.name}
                                            </p>
                                            <p className="text-muted-foreground mt-1 text-sm">
                                                <span className="marketing-data text-foreground">
                                                    {formatPackagePrice(
                                                        chosenPackage.price_cents,
                                                    )}
                                                </span>{' '}
                                                {packagePriceUnit(
                                                    chosenPackage,
                                                )}{' '}
                                                ·{' '}
                                                {packageCreditsLine(
                                                    chosenPackage,
                                                )}
                                                . Lo attivi dopo la
                                                registrazione.
                                            </p>
                                        </div>
                                    ) : null}
                                    <input
                                        type="hidden"
                                        name="package"
                                        value={packageSlug}
                                    />
                                </>
                            )}

                            <div className="grid gap-2">
                                <Label htmlFor="company_name">
                                    Nome ente / azienda
                                </Label>
                                <Input
                                    id="company_name"
                                    type="text"
                                    required
                                    autoFocus
                                    tabIndex={1}
                                    autoComplete="organization"
                                    name="company_name"
                                    placeholder="Nome ente / azienda"
                                />
                                <InputError
                                    message={errors.company_name}
                                    className="mt-2"
                                />
                            </div>

                            <div className="grid gap-2">
                                <Label htmlFor="name">Nome e cognome</Label>
                                <Input
                                    id="name"
                                    type="text"
                                    required
                                    tabIndex={2}
                                    autoComplete="name"
                                    name="name"
                                    placeholder="Nome e cognome"
                                />
                                <InputError
                                    message={errors.name}
                                    className="mt-2"
                                />
                            </div>

                            <div className="grid gap-2">
                                <Label htmlFor="email">Indirizzo email</Label>
                                <Input
                                    id="email"
                                    type="email"
                                    required
                                    tabIndex={3}
                                    autoComplete="email"
                                    name="email"
                                    placeholder="email@example.com"
                                />
                                <InputError message={errors.email} />
                            </div>

                            <div className="grid gap-2">
                                <Label htmlFor="password">Password</Label>
                                <PasswordInput
                                    id="password"
                                    required
                                    tabIndex={4}
                                    autoComplete="new-password"
                                    name="password"
                                    placeholder="Password"
                                    passwordrules={passwordRules}
                                />
                                <InputError message={errors.password} />
                            </div>

                            <div className="grid gap-2">
                                <Label htmlFor="password_confirmation">
                                    Conferma password
                                </Label>
                                <PasswordInput
                                    id="password_confirmation"
                                    required
                                    tabIndex={5}
                                    autoComplete="new-password"
                                    name="password_confirmation"
                                    placeholder="Conferma password"
                                    passwordrules={passwordRules}
                                />
                                <InputError
                                    message={errors.password_confirmation}
                                />
                            </div>

                            <Button
                                type="submit"
                                className="mt-2 w-full"
                                tabIndex={6}
                                data-test="register-user-button"
                            >
                                {processing && <Spinner />}
                                Crea account
                            </Button>
                        </div>

                        <div className="text-muted-foreground text-sm">
                            Hai già un account?{' '}
                            <TextLink href={login()} tabIndex={7}>
                                Accedi
                            </TextLink>
                        </div>
                    </>
                )}
            </Form>
        </>
    );
}

Register.layout = {
    title: 'Crea un account',
    description: 'Inserisci i tuoi dati per creare un account',
};
