// ---------------------------------------------------------------------------
// Ory Kratos browser self-service flow types
// ---------------------------------------------------------------------------
// Subset of the Kratos API schema covering the fields the honeyDue web client
// actually consumes. See https://www.ory.sh/docs/kratos/reference/api
// ---------------------------------------------------------------------------

/** Self-service flow categories supported by the UI. */
export type FlowType = 'login' | 'registration' | 'recovery' | 'verification' | 'settings';

/** A localized message attached to a node or to the flow as a whole. */
export interface KratosMessage {
  id: number;
  /** "info" | "error" | "success" */
  type: string;
  text: string;
  context?: Record<string, unknown>;
}

/** Attributes of a UI node — Kratos only emits the "input" group for our flows,
 *  but anchor/text/img/script groups are typed loosely for completeness. */
export interface KratosNodeAttributes {
  /** input node */
  name?: string;
  type?: string;
  value?: string | number | boolean;
  required?: boolean;
  disabled?: boolean;
  autocomplete?: string;
  label?: KratosMessage;
  /** "text" | "anchor" | "image" | "input" | "script" */
  node_type: string;
  // anchor / image
  href?: string;
  title?: KratosMessage;
  src?: string;
  // script
  id?: string;
}

/** A single renderable element of a Kratos flow form. */
export interface KratosUiNode {
  /** "default" | "password" | "oidc" | "code" | "totp" | "lookup_secret" | ... */
  group: string;
  type: string;
  attributes: KratosNodeAttributes;
  messages: KratosMessage[];
  meta: {
    label?: KratosMessage;
  };
}

/** The renderable form definition of a flow. */
export interface KratosUiContainer {
  /** Absolute URL to POST the form to. */
  action: string;
  /** "POST" (always, for these flows). */
  method: string;
  nodes: KratosUiNode[];
  /** Flow-level messages (e.g. "An email containing a code has been sent"). */
  messages?: KratosMessage[];
}

/** Common shape shared by all self-service flow responses. */
export interface KratosFlow {
  id: string;
  type: string;
  expires_at?: string;
  issued_at?: string;
  request_url?: string;
  /** Present on recovery / verification flows. */
  state?: string;
  ui: KratosUiContainer;
}

/** A Kratos identity (subset of fields used by the client). */
export interface KratosIdentity {
  id: string;
  schema_id: string;
  traits: Record<string, unknown>;
  verifiable_addresses?: Array<{
    value: string;
    verified: boolean;
    via: string;
    status: string;
  }>;
  metadata_public?: Record<string, unknown> | null;
}

/** Response of GET {kratos}/sessions/whoami. */
export interface KratosSession {
  id: string;
  active: boolean;
  expires_at?: string;
  authenticated_at?: string;
  identity: KratosIdentity;
}

/** Response of GET {kratos}/self-service/logout/browser. */
export interface KratosLogoutFlow {
  logout_url: string;
  logout_token: string;
}

/** Generic Kratos error envelope (e.g. from a 4xx with a JSON body). */
export interface KratosGenericError {
  error: {
    id?: string;
    code?: number;
    status?: string;
    reason?: string;
    message?: string;
  };
}
