"use client";
import { useActionState } from "react";
import { AlertCircle, Mail, Lock, Loader2 } from "lucide-react";
import { loginAction, type LoginState } from "./actions";
import { signInWithGoogleAction } from "./google-action";
import { Button } from "@/components/Button";

const initial: LoginState = { ok: false };

export function LoginForm({ googleEnabled }: { googleEnabled: boolean }) {
  const [state, action, pending] = useActionState(loginAction, initial);
  return (
    <div className="space-y-5">
      {googleEnabled && (
        <>
          <form action={signInWithGoogleAction}>
            <button
              type="submit"
              className="inline-flex w-full items-center justify-center gap-3 rounded-md border border-[var(--color-line)] bg-white px-4 py-2.5 text-sm font-semibold text-[var(--color-ink)] transition hover:bg-[var(--color-mist)] focus:outline-none focus:ring-2 focus:ring-[var(--color-blue)]/30"
            >
              <svg aria-hidden="true" width="18" height="18" viewBox="0 0 48 48">
                <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"/>
                <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"/>
                <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"/>
                <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"/>
              </svg>
              Continue with Google
            </button>
          </form>
          <div className="relative flex items-center gap-3 text-[11px] font-semibold uppercase tracking-wider text-[var(--color-slate)]">
            <span className="h-px flex-1 bg-[var(--color-line)]" />
            or
            <span className="h-px flex-1 bg-[var(--color-line)]" />
          </div>
        </>
      )}
      <form action={action} className="space-y-5">
      {state.message && (
        <div
          role="alert"
          className="flex items-start gap-2 rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-800"
        >
          <AlertCircle className="mt-0.5 h-4 w-4 shrink-0" aria-hidden="true" />
          <span>{state.message}</span>
        </div>
      )}
      <label className="block">
        <span className="mb-1.5 block text-sm font-semibold text-[var(--color-ink)]">
          Email
        </span>
        <div className="relative">
          <Mail
            className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-[var(--color-slate)]"
            aria-hidden="true"
          />
          <input
            name="email"
            type="email"
            required
            autoComplete="email"
            placeholder="you@hospital.org"
            className="w-full rounded-md border border-[var(--color-line)] bg-white py-2.5 pl-9 pr-3 text-[var(--color-ink)] placeholder:text-[var(--color-slate)]/60 focus:border-[var(--color-blue)] focus:outline-none focus:ring-2 focus:ring-[var(--color-blue)]/20"
          />
        </div>
      </label>
      <label className="block">
        <span className="mb-1.5 block text-sm font-semibold text-[var(--color-ink)]">
          Password
        </span>
        <div className="relative">
          <Lock
            className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-[var(--color-slate)]"
            aria-hidden="true"
          />
          <input
            name="password"
            type="password"
            required
            autoComplete="current-password"
            className="w-full rounded-md border border-[var(--color-line)] bg-white py-2.5 pl-9 pr-3 text-[var(--color-ink)] focus:border-[var(--color-blue)] focus:outline-none focus:ring-2 focus:ring-[var(--color-blue)]/20"
          />
        </div>
      </label>
      <Button type="submit" disabled={pending} className="w-full">
        {pending ? (
          <>
            <Loader2 className="h-4 w-4 animate-spin" aria-hidden="true" />
            Signing in...
          </>
        ) : (
          "Sign in"
        )}
      </Button>
      </form>
    </div>
  );
}
