export type RuntimeFieldType =
  | 'text'
  | 'textarea'
  | 'number'
  | 'email'
  | 'tel'
  | 'date'
  | 'select'
  | 'radio'
  | 'url'
  | 'file'
  | 'checkbox';

export type RuntimeOption = {
  label: string;
  value: string;
  imageUrl?: string;
};

export type RuntimeVisibilityCondition = {
  field: string;
  equals?: string;
  in?: string[];
};

export type RuntimeValidation = {
  minLength?: number;
  maxLength?: number;
  min?: number;
  max?: number;
  pattern?: string;
};

export type RuntimeOnboardingField = {
  id: string;
  label: string;
  type: RuntimeFieldType;
  required?: boolean;
  placeholder?: string;
  helperText?: string;
  defaultValue?: string | number | boolean | string[];
  readOnly?: boolean;
  /**
   * If true, this field (or document) becomes permanently read-only once the
   * profile's verification status reaches APPROVED — enforced server-side in
   * apps/users/src/handlers/profile.rs's save_profile, not just in this UI.
   */
  lockAfterApproval?: boolean;
  /**
   * When true, this field's value (or uploaded document URL) is considered a
   * shared identity credential (e.g. Aadhaar, PAN, selfie) that can be
   * pre-filled from the user's existing role profile when they register a
   * second role.  The user can always replace it.  Admins set this in the
   * Onboarding Schema Editor; the frontend also pre-fills fields whose IDs
   * match CONVENTION_IDENTITY_FIELD_IDS in lib/role-utils.ts as a fallback.
   */
  identity_shared?: boolean;
  multiple?: boolean;
  options?: RuntimeOption[];
  accept?: string;
  maxFiles?: number;
  maxSizeMB?: number;
  validation?: RuntimeValidation;
  visibleWhen?: RuntimeVisibilityCondition[];
};

export type RuntimeStepType = 'basic' | 'documents' | 'portfolio' | 'review';

export type RuntimeOnboardingStep = {
  id: string;
  title: string;
  description?: string;
  /** How the wizard should render this step; unrecognized/missing defaults to "basic". */
  type?: RuntimeStepType;
  visibleWhen?: RuntimeVisibilityCondition[];
  fields: RuntimeOnboardingField[];
};

export type RuntimePortfolioModel = 'none' | 'custom_data' | 'professional';

export type RuntimeOnboardingConfig = {
  schemaId: string;
  roleKey: string;
  version: number;
  steps: RuntimeOnboardingStep[];
  /** Which persistence model the portfolio step (if any) should save into. */
  portfolioModel?: RuntimePortfolioModel;
  /** Admin-configurable per-role toggle: render the wizard flow, or fall back to legacy tabs. */
  enableWizardFlow?: boolean;
};

export type UploadedFileMeta = {
  name: string;
  type: string;
  size: number;
  uploadedAt: string;
};

export type OnboardingSubmission = {
  id: string;
  schemaId: string;
  roleKey: string;
  submittedAt: string;
  values: Record<string, unknown>;
};

export type RuntimeKBArticle = {
  id: string;
  title: string;
  category: string;
  content: string;
  author: string;
  isPublished: boolean;
  viewCount: number;
  lastUpdated: string;
};

export type RuntimeKBConfig = {
  articles: RuntimeKBArticle[];
};
