import { Button, Input, PinInput } from "@wrnexus/ui"

component OtpSignIn {
  props {
    requestAction = "/api/auth/otp/login/request"
    requestSchema = "auth-otp-login-request"
    completeAction = "/api/auth/otp/login/complete"
    completeSchema = "auth-otp-login-complete"
    challengeId = ""
    returnTo = "/"
    mfaHref = "/two-factor"
    method = "email-otp"
    title = "Sign in with a code"
    description = "We will send a one-time code to your email address or phone number."
    identifierLabel = "Email address or phone number"
    requestLabel = "Send code"
    verifyLabel = "Verify and sign in"
    successMessage = "A one-time code has been sent."
    color = "primary"
    size = "md"
    class = ""
  }
  view {
    <section
      {...attrs}
      data-wrnexus-runtime="auth"
      data-auth-otp-sign-in
      data-mfa-href='{mfaHref}'
      class='w-full max-w-md rounded-[var(--wire-radius-lg)] border border-[var(--wire-color-border)] bg-[var(--wire-color-surface)] p-6 text-[var(--wire-color-text)] shadow-[var(--wire-shadow-2)] {class}'
    >
      <div class="mb-5 flex size-11 items-center justify-center rounded-full bg-[color-mix(in_srgb,var(--wire-color-primary)_12%,transparent)] text-[var(--wire-color-primary)]">
        <span class="icon-[lucide--key-round] size-5"></span>
      </div>
      <h1 class="m-0 text-2xl font-semibold tracking-tight">{title}</h1>
      <p class="mb-0 mt-2 text-sm text-[var(--wire-color-muted)]">{description}</p>

      <form
        method="post"
        action='{requestAction}'
        data-schema='{requestSchema}'
        data-auth-otp-request
        novalidate
        class='mt-6 space-y-4 {challengeId == "" ? "" : "hidden"}'
      >
        <input type="hidden" name="method" value='{method}' />
        <Input id="auth-otp-identifier" name="identifier" type="text" label="{identifierLabel}" autocomplete="username" placeholder="Enter your email address or phone number" icon="icon-[lucide--at-sign]" color="{color}" size="{size}" />
        <p data-error="method" class="m-0 min-h-4 text-xs text-[var(--wire-color-danger)]"></p>
        <p data-error="_form" role="alert" class="m-0 hidden rounded-[var(--wire-radius-sm)] bg-[color-mix(in_srgb,var(--wire-color-danger)_10%,transparent)] p-3 text-sm text-[var(--wire-color-danger)]"></p>
        <p data-success='{successMessage}' role="status" hidden class="m-0 rounded-[var(--wire-radius-sm)] bg-[color-mix(in_srgb,var(--wire-color-success)_10%,transparent)] p-3 text-sm text-[var(--wire-color-success)]">{successMessage}</p>
        <Button type="submit" label="{requestLabel}" variant="solid" color="{color}" size="{size}" fullWidth="true" />
      </form>

      <form
        method="post"
        action='{completeAction}'
        data-schema='{completeSchema}'
        data-auth-otp-complete
        novalidate
        class='mt-6 space-y-4 {challengeId == "" ? "hidden" : ""}'
      >
        <input type="hidden" name="challengeId" value='{challengeId}' />
        <input type="hidden" name="returnTo" value='{returnTo}' />
        <PinInput label="One-time code" name="code" length="6" inputMode="numeric" autocomplete="one-time-code" color="{color}" size="{size}" />
        <p data-error="challengeId" class="m-0 min-h-4 text-xs text-[var(--wire-color-danger)]"></p>
        <p data-error="_form" role="alert" class="m-0 hidden rounded-[var(--wire-radius-sm)] bg-[color-mix(in_srgb,var(--wire-color-danger)_10%,transparent)] p-3 text-sm text-[var(--wire-color-danger)]"></p>
        <Button type="submit" label="{verifyLabel}" variant="solid" color="{color}" size="{size}" fullWidth="true" />
        <Button type="button" data-auth-otp-back label="Use a different account" variant="ghost" color="{color}" size="{size}" fullWidth="true" />
      </form>
    </section>
  }
}
