import { Head } from '@inertiajs/react';
import PasskeyVerify from '@/components/passkey-verify';
import { SignInForm } from '@/components/crm/sign-in-form';
import { Button } from '@/components/ui/button';

type Props = {
    status?: string;
    canResetPassword: boolean;
    /** Offered only when this installation has a Google client configured. */
    google?: {
        enabled: boolean;
        url: string;
    };
    /** Why a Google sign-in was turned away, if it was. */
    oauthError?: string | null;
};

/**
 * The staff way in.
 *
 * A passkey first where the browser supports one, then the work email and
 * password, so a crew member on a phone has the shortest route and an
 * administrator at a desk still has the familiar one. Google sits above both
 * when the business has set it up: it signs somebody in to the account they
 * already hold here, it never creates one. There is no sign-up link: staff
 * accounts are provisioned by an administrator, not created by whoever finds
 * the address.
 */
export default function Login({
    status,
    canResetPassword,
    google,
    oauthError,
}: Props) {
    return (
        <>
            <Head title="Staff sign in" />

            <div className="grid gap-5">
                {oauthError && (
                    <div
                        role="alert"
                        className="rounded-lg bg-destructive/10 px-3 py-2.5 text-sm text-foreground ring-1 ring-destructive/30 ring-inset"
                    >
                        {oauthError}
                    </div>
                )}

                {status && (
                    <div
                        role="status"
                        className="rounded-lg bg-success-soft px-3 py-2.5 text-sm text-foreground ring-1 ring-success/30 ring-inset"
                    >
                        {status}
                    </div>
                )}

                {google?.enabled && (
                    <div className="grid gap-2">
                        <Button asChild variant="outline" className="w-full">
                            {/* A real link, not an Inertia visit: the next page
                                is Google's, on another origin. */}
                            <a href={google.url} rel="nofollow">
                                <GoogleMark />
                                Continue with Google
                            </a>
                        </Button>
                    </div>
                )}

                <PasskeyVerify
                    label="Sign in with a passkey"
                    separator="or sign in with your email"
                    separatorClassName="bg-card"
                />

                <SignInForm
                    canResetPassword={canResetPassword}
                    autoFocus={false}
                />
            </div>
        </>
    );
}

/**
 * Google's own mark, in its own colours: a monochrome "G" next to the familiar
 * one is the kind of detail that makes a sign-in button look counterfeit.
 */
function GoogleMark() {
    return (
        <svg viewBox="0 0 24 24" aria-hidden="true" className="size-4">
            <path
                fill="#4285F4"
                d="M23.06 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h6.19a5.3 5.3 0 0 1-2.3 3.48v2.9h3.72c2.18-2 3.45-4.95 3.45-8.39Z"
            />
            <path
                fill="#34A853"
                d="M12 24c3.1 0 5.7-1.03 7.6-2.79l-3.72-2.9c-1.03.69-2.35 1.1-3.88 1.1-2.99 0-5.52-2.02-6.43-4.73H1.72v2.99A11.99 11.99 0 0 0 12 24Z"
            />
            <path
                fill="#FBBC05"
                d="M5.57 14.68a7.2 7.2 0 0 1 0-4.6V7.09H1.72a12 12 0 0 0 0 10.58l3.85-2.99Z"
            />
            <path
                fill="#EA4335"
                d="M12 4.75c1.69 0 3.2.58 4.4 1.72l3.29-3.29C17.7 1.19 15.1 0 12 0 7.31 0 3.26 2.69 1.72 6.6l3.85 3c.91-2.71 3.44-4.85 6.43-4.85Z"
            />
        </svg>
    );
}

// The card's heading and sentence are company settings rather than constants:
// they are written in Settings -> Company profile, so this page deliberately
// hands the layout no title and lets the company's own words stand.
