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

export const MOCK_USERS: MockUser[] = [
  { name: '陈销售', role: 'sales', department: '销售一部' },
  { name: '李法务', role: 'legal', department: '法务部' },
  { name: '张财务', role: 'finance', department: '财务部' },
  { name: '王业管', role: 'businessAdmin', department: '业管部' },
  { name: '赵运维', role: 'ops', department: '运维资产部' },
  { name: '刘风控', role: 'riskControl', department: '风控部' },
  { name: '周管理', role: 'manager', department: '管理层' },
  { name: '系统管理员', role: 'admin', department: '系统管理部' },
];

export const DEFAULT_MOCK_USER = MOCK_USERS[1];

const TAG_MAINTAINER_ROLES: UserRole[] = ['legal', 'finance', 'businessAdmin', 'admin'];

export function roleLabel(role: UserRole): string {
  return ROLE_LABELS[role];
}

export function canViewRiskTags(_user: MockUser): boolean {
  return true;
}

export function canManageRiskTags(user: MockUser): boolean {
  return TAG_MAINTAINER_ROLES.includes(user.role);
}

export function canAddRiskTags(user: MockUser): boolean {
  if (user.role === 'sales' || user.role === 'ops') return false;
  if (user.role === 'riskControl' || user.role === 'manager') return false;
  return TAG_MAINTAINER_ROLES.includes(user.role);
}

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

/** 分区维护权限：信用与回款/开票异常→财务；合规与法律→法务；经营与履约/多头签约→业管 */
export function canMaintainCategory(user: MockUser, category: TagCategoryCode): boolean {
  if (user.role === 'admin') return true;
  if (user.role === 'legal' && category === 'legal') return true;
  if (user.role === 'finance' && category === 'finance') return true;
  if (user.role === 'businessAdmin' && category === 'business') return true;
  return false;
}

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

export function canRemoveRiskTag(user: MockUser, category: TagCategoryCode): boolean {
  return canMaintainCategory(user, category);
}

export function canApplyRiskTag(user: MockUser, tag: TagCatalogItem): boolean {
  if (!canAddRiskTags(user)) return false;
  if (user.role === 'admin') return true;
  return tag.editableRoles.includes(user.role);
}

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

export function roleToTagCategory(user: MockUser): TagCategoryCode {
  return ROLE_TO_DEFAULT_CATEGORY[user.role] ?? 'legal';
}

export function canViewFinanceSensitive(user: MockUser): boolean {
  return ['finance', 'manager', 'admin'].includes(user.role);
}

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

export function selectableCategoriesForRole(user: MockUser): TagCategoryCode[] {
  if (user.role === 'admin') return ['legal', 'finance', 'business'];
  const cat = ROLE_TO_DEFAULT_CATEGORY[user.role];
  return cat ? [cat] : [];
}

export function getUserByRole(role: UserRole): MockUser {
  return MOCK_USERS.find((item) => item.role === role) ?? DEFAULT_MOCK_USER;
}
