import type {
  RiskLevelCode,
  SensitiveLevel,
  TagCategoryCode,
  UserRole,
} from '../types/riskTag';

/** 业务分组（与规范文档四张表一致） */
export type TagGroupCode = 'credit' | 'compliance' | 'operation' | 'process';

export interface TagCatalogItem {
  tagCode: string;
  tagName: string;
  tagGroup: TagGroupCode;
  groupName: string;
  tagCategory: TagCategoryCode;
  tagCategoryName: string;
  responsibleDept: string;
  editableRoles: UserRole[];
  defaultLevel: RiskLevelCode;
  requiresApproval: boolean;
  triggersBlacklist: boolean;
  reviewCycle: string;
  sensitiveLevel: SensitiveLevel;
}

export const TAG_GROUP_LABELS: Record<TagGroupCode, string> = {
  credit: '信用与回款',
  compliance: '合规与法律',
  operation: '经营与履约',
  process: '操作与流程',
};

export const TAG_CATEGORY_META: Record<
  TagCategoryCode,
  { name: string; dept: string; description: string; order: number }
> = {
  legal: {
    name: '法务风险',
    dept: '法务部',
    description: '由法务维护，关注资质、涉诉、制裁、合规风险（合规与法律）',
    order: 1,
  },
  finance: {
    name: '财务风险',
    dept: '财务部',
    description: '由财务维护，关注信用与回款、开票异常等财务类风险',
    order: 2,
  },
  business: {
    name: '业管风险',
    dept: '业管部',
    description: '由业管维护，关注经营履约、车辆/资产、信息核实及多头签约',
    order: 3,
  },
};

export const RISK_LEVEL_LABELS: Record<RiskLevelCode, string> = {
  low: '低',
  medium: '中',
  high: '高',
  fatal: '致命',
};

export const TAG_STATUS_LABELS: Record<
  import('../types/riskTag').TagStatusCode,
  string
> = {
  active: '生效中',
  pending: '待审批',
  expired: '已失效',
  removed: '已解除',
  rejected: '已驳回',
};

export const SOURCE_LABELS = {
  system: '系统生成',
  manual: '人工维护',
} as const;

export const REVIEW_CYCLE_OPTIONS: import('../types/riskTag').ReviewCycleOption[] = [
  '1个月',
  '2个月',
  '3个月',
  '6个月',
];

export const REVIEW_CONCLUSION_LABELS: Record<
  import('../types/riskTag').ReviewConclusionCode,
  string
> = {
  riskExists: '风险仍存在',
  riskResolved: '风险已解除',
  pendingConfirmation: '待进一步确认',
};

/** 标签目录 — 仅规范文档定义的 11 项 */
export const TAG_CATALOG: TagCatalogItem[] = [
  // 信用与回款 → 财务
  {
    tagCode: 'CR-01',
    tagName: '逾期预警',
    tagGroup: 'credit',
    groupName: '信用与回款',
    tagCategory: 'finance',
    tagCategoryName: '财务风险',
    responsibleDept: '财务部',
    editableRoles: ['finance', 'admin'],
    defaultLevel: 'medium',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '1个月',
    sensitiveLevel: 'financeSensitive',
  },
  {
    tagCode: 'CR-02',
    tagName: '严重拖欠',
    tagGroup: 'credit',
    groupName: '信用与回款',
    tagCategory: 'finance',
    tagCategoryName: '财务风险',
    responsibleDept: '财务部',
    editableRoles: ['finance', 'admin'],
    defaultLevel: 'high',
    requiresApproval: true,
    triggersBlacklist: true,
    reviewCycle: '1个月',
    sensitiveLevel: 'financeSensitive',
  },
  {
    tagCode: 'CR-03',
    tagName: '频繁改账期',
    tagGroup: 'credit',
    groupName: '信用与回款',
    tagCategory: 'finance',
    tagCategoryName: '财务风险',
    responsibleDept: '财务部',
    editableRoles: ['finance', 'admin'],
    defaultLevel: 'low',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '1个月',
    sensitiveLevel: 'financeSensitive',
  },
  // 合规与法律 → 法务
  {
    tagCode: 'CP-01',
    tagName: '资质存疑',
    tagGroup: 'compliance',
    groupName: '合规与法律',
    tagCategory: 'legal',
    tagCategoryName: '法务风险',
    responsibleDept: '法务部',
    editableRoles: ['legal', 'admin'],
    defaultLevel: 'medium',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '3个月',
    sensitiveLevel: 'normal',
  },
  {
    tagCode: 'CP-02',
    tagName: '涉诉/仲裁',
    tagGroup: 'compliance',
    groupName: '合规与法律',
    tagCategory: 'legal',
    tagCategoryName: '法务风险',
    responsibleDept: '法务部',
    editableRoles: ['legal', 'admin'],
    defaultLevel: 'high',
    requiresApproval: true,
    triggersBlacklist: true,
    reviewCycle: '3个月',
    sensitiveLevel: 'legalSensitive',
  },
  {
    tagCode: 'CP-03',
    tagName: '制裁/高风险地区',
    tagGroup: 'compliance',
    groupName: '合规与法律',
    tagCategory: 'legal',
    tagCategoryName: '法务风险',
    responsibleDept: '法务部',
    editableRoles: ['legal', 'admin'],
    defaultLevel: 'high',
    requiresApproval: true,
    triggersBlacklist: true,
    reviewCycle: '3个月',
    sensitiveLevel: 'legalSensitive',
  },
  // 经营与履约 → 业管
  {
    tagCode: 'OP-01',
    tagName: '频繁争议',
    tagGroup: 'operation',
    groupName: '经营与履约',
    tagCategory: 'business',
    tagCategoryName: '业管风险',
    responsibleDept: '业管部',
    editableRoles: ['businessAdmin', 'admin'],
    defaultLevel: 'medium',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '6个月',
    sensitiveLevel: 'normal',
  },
  {
    tagCode: 'OP-02',
    tagName: '车辆/资产高风险',
    tagGroup: 'operation',
    groupName: '经营与履约',
    tagCategory: 'business',
    tagCategoryName: '业管风险',
    responsibleDept: '业管部',
    editableRoles: ['businessAdmin', 'admin'],
    defaultLevel: 'high',
    requiresApproval: true,
    triggersBlacklist: false,
    reviewCycle: '6个月',
    sensitiveLevel: 'normal',
  },
  {
    tagCode: 'OP-03',
    tagName: '信息不实',
    tagGroup: 'operation',
    groupName: '经营与履约',
    tagCategory: 'business',
    tagCategoryName: '业管风险',
    responsibleDept: '业管部',
    editableRoles: ['businessAdmin', 'admin'],
    defaultLevel: 'medium',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '6个月',
    sensitiveLevel: 'normal',
  },
  // 操作与流程 → 开票异常归财务，多头签约归业管
  {
    tagCode: 'PR-01',
    tagName: '开票异常',
    tagGroup: 'process',
    groupName: '操作与流程',
    tagCategory: 'finance',
    tagCategoryName: '财务风险',
    responsibleDept: '财务部',
    editableRoles: ['finance', 'admin'],
    defaultLevel: 'low',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '1个月',
    sensitiveLevel: 'financeSensitive',
  },
  {
    tagCode: 'PR-02',
    tagName: '多头签约',
    tagGroup: 'process',
    groupName: '操作与流程',
    tagCategory: 'business',
    tagCategoryName: '业管风险',
    responsibleDept: '业管部',
    editableRoles: ['businessAdmin', 'admin'],
    defaultLevel: 'low',
    requiresApproval: false,
    triggersBlacklist: false,
    reviewCycle: '6个月',
    sensitiveLevel: 'normal',
  },
];

/** 表单下拉分组（按规范文档顺序） */
export const TAG_GROUPS: Array<{ groupName: string; tagNames: string[] }> = [
  { groupName: '信用与回款', tagNames: ['逾期预警', '严重拖欠', '频繁改账期'] },
  { groupName: '合规与法律', tagNames: ['资质存疑', '涉诉/仲裁', '制裁/高风险地区'] },
  { groupName: '经营与履约', tagNames: ['频繁争议', '车辆/资产高风险', '信息不实'] },
  { groupName: '操作与流程', tagNames: ['开票异常', '多头签约'] },
];

export const LEGACY_TAG_ALIASES: Record<string, string> = {};

export const TAG_CATALOG_MAP = Object.fromEntries(
  TAG_CATALOG.map((item) => [item.tagName, item]),
) as Record<string, TagCatalogItem>;

export const TAG_CATALOG_BY_CODE = Object.fromEntries(
  TAG_CATALOG.map((item) => [item.tagCode, item]),
) as Record<string, TagCatalogItem>;

export const TAG_CATEGORY_ORDER: TagCategoryCode[] = ['legal', 'finance', 'business'];

export const HIGH_RISK_LEVELS: RiskLevelCode[] = ['high', 'fatal'];

export const ROLE_LABELS: Record<UserRole, string> = {
  sales: '销售',
  legal: '法务',
  finance: '财务',
  businessAdmin: '业管',
  ops: '运维/资产',
  riskControl: '风控',
  manager: '管理层',
  admin: '系统管理员',
};

export const ROLE_TO_DEFAULT_CATEGORY: Partial<Record<UserRole, TagCategoryCode>> = {
  legal: 'legal',
  finance: 'finance',
  businessAdmin: 'business',
};

export function tagsForCategory(category: TagCategoryCode): TagCatalogItem[] {
  return TAG_CATALOG.filter((item) => item.tagCategory === category);
}

export function tagsForRole(userRole: UserRole): TagCatalogItem[] {
  if (userRole === 'admin') return TAG_CATALOG;
  if (userRole === 'legal') return tagsForCategory('legal');
  if (userRole === 'finance') return tagsForCategory('finance');
  if (userRole === 'businessAdmin') return tagsForCategory('business');
  return [];
}
