import type { MockUser, RiskTagCategory, UserRole } from '../types/riskTag';
import type { TagCatalogItem } from './riskTagConstants';
import { TAG_CATALOG_MAP } from './riskTagConstants';

export const MOCK_USERS: MockUser[] = [
  { name: '王风控', role: 'risk', department: '风控部' },
  { name: '李法务', role: 'legal', department: '法务部' },
  { name: '张财务', role: 'finance', department: '财务部' },
  { name: '赵运维', role: 'ops', department: '运维部' },
  { name: '陈销售', role: 'sales', department: '销售一部' },
];

export const DEFAULT_MOCK_USER = MOCK_USERS[0];

export function canManageRiskTags(user: MockUser): boolean {
  return ['finance', 'legal', 'risk', 'ops', 'admin'].includes(user.role);
}

export function canBatchTag(user: MockUser): boolean {
  return canManageRiskTags(user);
}

export function canApproveRiskTag(user: MockUser): boolean {
  return ['legal', 'risk', 'admin'].includes(user.role);
}

export function canRemoveRiskTag(user: MockUser, tagType: RiskTagCategory): boolean {
  if (user.role === 'admin' || user.role === 'risk') return true;
  if (user.role === 'legal' && tagType === '法务') return true;
  if (user.role === 'finance' && tagType === '财务') return true;
  if (user.role === 'ops' && tagType === '运维') return true;
  return false;
}

/** 当前角色对应的标签类型（打标表单自动带出） */
export function roleToTagCategory(user: MockUser): RiskTagCategory {
  const map: Record<UserRole, RiskTagCategory> = {
    finance: '财务',
    legal: '法务',
    ops: '运维',
    risk: '风控',
    sales: '风控',
    admin: '风控',
  };
  return map[user.role];
}

/** 是否可选择该标签（按标签业务类型与角色匹配） */
export function canApplyRiskTag(user: MockUser, tag: TagCatalogItem): boolean {
  if (user.role === 'admin' || user.role === 'risk') return true;
  if (user.role === 'legal' && tag.tagType === '法务') return true;
  if (user.role === 'finance' && tag.tagType === '财务') return true;
  if (user.role === 'ops' && tag.tagType === '运维') return true;
  return false;
}

export function canApplyRiskTagByName(user: MockUser, tagName: string): boolean {
  const tag = TAG_CATALOG_MAP[tagName];
  if (!tag) return false;
  return canApplyRiskTag(user, tag);
}

export function roleLabel(role: UserRole): string {
  const map: Record<UserRole, string> = {
    finance: '财务',
    legal: '法务',
    risk: '风控',
    ops: '运维',
    sales: '销售',
    admin: '管理员',
  };
  return map[role];
}
