import { ORG_ROLE_LABELS, PERMISSION_ROLE_LABELS, type OrgRole, type PermissionRole } from './constants';

export function fmtDate(iso: string): string {
	if (!iso) return '—';
	const d = new Date(iso);
	if (Number.isNaN(d.getTime())) return iso;
	return d.toLocaleString(undefined, {
		year: 'numeric',
		month: 'short',
		day: '2-digit',
		hour: '2-digit',
		minute: '2-digit',
	});
}

export function fmtDateOnly(iso: string): string {
	if (!iso) return '—';
	const d = new Date(iso);
	if (Number.isNaN(d.getTime())) return iso;
	return d.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: '2-digit' });
}

export function fmtCost(usd: number | null): string {
	if (usd === null) return '—';
	return `$${Number(usd).toFixed(4)}`;
}

export function fmtNum(n: number): string {
	return n.toLocaleString();
}

export function orgRoleLabel(role: string): string {
	const key = role.toUpperCase() as OrgRole;
	return ORG_ROLE_LABELS[key] ?? role;
}

export function permissionRoleLabel(role: string): string {
	const key = role.toUpperCase() as PermissionRole;
	return PERMISSION_ROLE_LABELS[key] ?? role;
}

export function providerModeLabel(mode: string): string {
	if (mode === 'BYOK') return '自带密钥';
	if (mode === 'PLATFORM_MANAGED') return '平台托管';
	return mode;
}
