export const TENANT_ROLES = [
  'platform_admin',
  'tenant_owner',
  'tenant_admin',
  'tenant_operator',
  'teacher',
  'sales',
  'agent',
  'student',
] as const;

export type TenantRole = (typeof TENANT_ROLES)[number];

export const QUESTION_STATUSES = ['draft', 'published', 'archived'] as const;
export type QuestionStatus = (typeof QUESTION_STATUSES)[number];

export const ORDER_STATUSES = ['pending', 'paid', 'failed', 'closed', 'refunded'] as const;
export type OrderStatus = (typeof ORDER_STATUSES)[number];

export const ENTITLEMENT_SCOPE_TYPES = ['tenant', 'region', 'module', 'subject', 'question_bank'] as const;
export type EntitlementScopeType = (typeof ENTITLEMENT_SCOPE_TYPES)[number];

export function normalizeTenantRole(role: unknown): TenantRole {
  const value = String(role || '').toLowerCase();
  if (value === 'superadmin') return 'platform_admin';
  if (value === 'admin') return 'tenant_admin';
  if (value === 'operator') return 'tenant_operator';
  if (TENANT_ROLES.includes(value as TenantRole)) return value as TenantRole;
  return 'student';
}

export function normalizeOrderStatus(status: unknown): OrderStatus {
  const value = String(status || '').toLowerCase();
  if (value === 'paid' || value === 'success') return 'paid';
  if (value === 'failed' || value === 'fail') return 'failed';
  if (value === 'refunded' || value === 'refund') return 'refunded';
  if (value === 'closed' || value === 'cancelled' || value === 'canceled') return 'closed';
  return 'pending';
}
