import { Form, Head, Link } from '@inertiajs/react';
import { Info } from 'lucide-react';
import InputError from '@/components/input-error';
import { TicketCard } from '@/components/ticket/ticket-ui';
import { Button } from '@/components/ui/button';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';

type Props = {
    states: Array<{ value: string; label: string }>;
};

const selectClassName =
    'border-input h-9 w-full rounded-md border bg-transparent px-3 text-sm shadow-xs outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]';

export default function TicketRegister({ states }: Props) {
    return (
        <>
            <Head title="Create an account" />

            <div className="mx-auto w-full max-w-xl">
                <h1 className="text-2xl font-semibold tracking-tight">
                    Create your ticket account
                </h1>
                <p className="mt-2 text-sm text-muted-foreground">
                    If we installed your system, we will match your details to
                    your account and fill in the rest. If we have not met yet,
                    this creates a customer record for you.
                </p>

                <TicketCard className="mt-6">
                    <Form
                        action="/ticket/register"
                        method="post"
                        resetOnSuccess={[
                            'password',
                            'password_confirmation',
                        ]}
                        className="flex flex-col gap-5"
                    >
                        {({ processing, errors }) => (
                            <>
                                <div className="grid gap-4 sm:grid-cols-2">
                                    <div className="grid gap-2 sm:col-span-2">
                                        <Label htmlFor="name">Full name</Label>
                                        <Input
                                            id="name"
                                            name="name"
                                            required
                                            autoFocus
                                            autoComplete="name"
                                            placeholder="Jane Citizen"
                                        />
                                        <InputError message={errors.name} />
                                    </div>

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

                                    <div className="grid gap-2">
                                        <Label htmlFor="phone">
                                            Mobile or phone
                                        </Label>
                                        <Input
                                            id="phone"
                                            name="phone"
                                            type="tel"
                                            required
                                            autoComplete="tel"
                                            placeholder="04xx xxx xxx"
                                        />
                                        <InputError message={errors.phone} />
                                    </div>

                                    <div className="grid gap-2">
                                        <Label htmlFor="password">
                                            Password
                                        </Label>
                                        <Input
                                            id="password"
                                            name="password"
                                            type="password"
                                            required
                                            autoComplete="new-password"
                                        />
                                        <InputError message={errors.password} />
                                    </div>

                                    <div className="grid gap-2">
                                        <Label htmlFor="password_confirmation">
                                            Confirm password
                                        </Label>
                                        <Input
                                            id="password_confirmation"
                                            name="password_confirmation"
                                            type="password"
                                            required
                                            autoComplete="new-password"
                                        />
                                        <InputError
                                            message={
                                                errors.password_confirmation
                                            }
                                        />
                                    </div>
                                </div>

                                <div className="rounded-xl bg-muted/50 p-4">
                                    <p className="text-xs font-medium">
                                        Is this a new address for us? (optional)
                                    </p>
                                    <p className="mt-1 text-xs text-muted-foreground">
                                        Existing customers already have their
                                        installation address on file. We only
                                        use this when we have no record of you.
                                    </p>

                                    <div className="mt-3 grid gap-3 sm:grid-cols-2">
                                        <div className="grid gap-2 sm:col-span-2">
                                            <Label htmlFor="address_line1">
                                                Street address
                                            </Label>
                                            <Input
                                                id="address_line1"
                                                name="address_line1"
                                                autoComplete="address-line1"
                                                placeholder="12 Example Street"
                                            />
                                            <InputError
                                                message={errors.address_line1}
                                            />
                                        </div>

                                        <div className="grid gap-2">
                                            <Label htmlFor="suburb">
                                                Suburb
                                            </Label>
                                            <Input
                                                id="suburb"
                                                name="suburb"
                                                autoComplete="address-level2"
                                            />
                                            <InputError
                                                message={errors.suburb}
                                            />
                                        </div>

                                        <div className="grid grid-cols-2 gap-3">
                                            <div className="grid gap-2">
                                                <Label htmlFor="state">
                                                    State
                                                </Label>
                                                <select
                                                    id="state"
                                                    name="state"
                                                    className={selectClassName}
                                                    defaultValue=""
                                                >
                                                    <option value="">
                                                        Select
                                                    </option>
                                                    {states.map((state) => (
                                                        <option
                                                            key={state.value}
                                                            value={state.value}
                                                        >
                                                            {state.label}
                                                        </option>
                                                    ))}
                                                </select>
                                                <InputError
                                                    message={errors.state}
                                                />
                                            </div>

                                            <div className="grid gap-2">
                                                <Label htmlFor="postcode">
                                                    Postcode
                                                </Label>
                                                <Input
                                                    id="postcode"
                                                    name="postcode"
                                                    inputMode="numeric"
                                                    autoComplete="postal-code"
                                                />
                                                <InputError
                                                    message={errors.postcode}
                                                />
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div className="rounded-xl border border-hairline p-4">
                                    <div className="flex items-start gap-2.5">
                                        <Info className="mt-0.5 size-4 shrink-0 text-muted-foreground" />
                                        <div className="text-xs leading-relaxed text-muted-foreground">
                                            <p className="font-medium text-foreground">
                                                How we handle your details
                                            </p>
                                            <p className="mt-1">
                                                We collect your name, contact
                                                details, property address,
                                                system details and anything you
                                                send us, to answer your report
                                                and arrange service, warranty or
                                                network work. We share only what
                                                is needed with the installer,
                                                manufacturer, distributor or
                                                network involved — never for
                                                marketing without your consent.
                                                You can ask to see or correct
                                                your details at any time by
                                                contacting us. Full details are
                                                in our privacy policy.
                                            </p>
                                        </div>
                                    </div>

                                    <div className="mt-3 flex items-start gap-2.5">
                                        <Checkbox
                                            id="privacy_consent"
                                            name="privacy_consent"
                                            className="mt-0.5"
                                        />
                                        <div>
                                            <Label htmlFor="privacy_consent">
                                                I agree to my details being
                                                collected and used as described
                                            </Label>
                                            <InputError
                                                message={
                                                    errors.privacy_consent
                                                }
                                            />
                                        </div>
                                    </div>
                                </div>

                                <Button
                                    type="submit"
                                    className="w-full"
                                    disabled={processing}
                                >
                                    {processing && <Spinner />}
                                    Create account
                                </Button>
                            </>
                        )}
                    </Form>
                </TicketCard>

                <p className="mt-5 text-center text-sm text-muted-foreground">
                    Already registered?{' '}
                    <Link
                        href="/ticket/login"
                        className="font-medium text-foreground underline underline-offset-4"
                    >
                        Sign in
                    </Link>
                </p>
            </div>
        </>
    );
}
