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

component AuthenticatorSetup {
  props {
    setupAction = "/api/auth/totp/setup"
    setupSchema = "auth-authenticator-setup"
    action = "/api/auth/totp/confirm"
    schema = "auth-authenticator-confirm"
    credentialId = ""
    secret = ""
    uri = ""
    title = "Set up an authenticator"
    description = "Create a setup key, add it to your authenticator app, then enter a current code."
    setupLabel = "Create setup key"
    submitLabel = "Enable authenticator"
    successMessage = "Authenticator enabled."
    color = "primary"
    size = "md"
    class = ""
  }
  view {
    <section
      {...attrs}
      data-wrnexus-runtime="auth"
      data-auth-authenticator-setup
      class='w-full max-w-lg rounded-[var(--wire-radius-lg)] border border-[var(--wire-color-border)] bg-[var(--wire-color-surface)] p-6 text-[var(--wire-color-text)] {class}'
    >
      <div class="flex items-start gap-4">
        <span class="flex size-11 shrink-0 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--scan-line] size-5"></span></span>
        <div><h1 class="m-0 text-xl font-semibold">{title}</h1><p class="mb-0 mt-1 text-sm text-[var(--wire-color-muted)]">{description}</p></div>
      </div>

      <form
        method="post"
        action='{setupAction}'
        data-schema='{setupSchema}'
        data-auth-authenticator-start
        novalidate
        class='mt-5 space-y-4 {credentialId == "" ? "" : "hidden"}'
      >
        <Input id="auth-authenticator-label" name="label" label="Authenticator name" value="Authenticator" autocomplete="off" icon="icon-[lucide--smartphone]" color="{color}" size="{size}" />
        <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="{setupLabel}" variant="solid" color="{color}" size="{size}" fullWidth="true" />
      </form>

      <div data-auth-authenticator-details class='{credentialId == "" ? "hidden" : ""}'>
        <div class="mt-5 rounded-[var(--wire-radius-sm)] bg-[var(--wire-color-surface-2)] p-4">
          <p class="m-0 text-xs font-semibold uppercase tracking-wide text-[var(--wire-color-muted)]">Manual setup key</p>
          <code data-auth-authenticator-secret class="mt-2 block break-all text-sm font-semibold tracking-wider">{secret}</code>
          <code data-auth-authenticator-uri class="mt-3 block max-h-24 overflow-auto break-all text-xs text-[var(--wire-color-muted)]">{uri}</code>
        </div>
        <form method="post" action='{action}' data-schema='{schema}' data-auth-authenticator-confirm novalidate class="mt-5 space-y-4">
          <input type="hidden" name="credentialId" value='{credentialId}' />
          <PinInput label="Authenticator code" name="code" length="6" inputMode="numeric" autocomplete="one-time-code" color="{color}" size="{size}" />
          <p data-error="credentialId" 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="{submitLabel}" variant="solid" color="{color}" size="{size}" fullWidth="true" />
        </form>
      </div>
    </section>
  }
}
