export const PRESET_PERMISSIONS: Array<{ code: string; name: string; group: string }> = [
  { code: 'dashboard:view', name: '查看数据面板', group: 'dashboard' },
  { code: 'notification:view', name: '查看通知', group: 'notification' },
  { code: 'student:view', name: '查看学生管理', group: 'student' },
  { code: 'student:basic-view', name: '查看学生基础信息', group: 'student-scope' },
  { code: 'teacher-workspace:view', name: '查看教师工作台', group: 'teacher-workspace' },
  { code: 'teacher:view', name: '查看教师', group: 'teacher' },
  { code: 'teacher:edit', name: '编辑教师', group: 'teacher' },
  { code: 'student:create', name: '新增学生', group: 'student' },
  { code: 'student:edit', name: '编辑学生', group: 'student' },
  { code: 'student:delete', name: '归档学生', group: 'student' },
  { code: 'student:import', name: '导入学生', group: 'student' },
  { code: 'student:export', name: '导出学生', group: 'student' },
  { code: 'exam:view', name: '查看和录入考试成绩', group: 'exam' },
  { code: 'room:view', name: '查看宿舍', group: 'room' },
  { code: 'room:inspect', name: '宿舍查寝', group: 'room' },
  { code: 'room:create', name: '新增宿舍', group: 'room' },
  { code: 'room:edit', name: '编辑宿舍', group: 'room' },
  { code: 'room:delete', name: '归档宿舍', group: 'room' },
  { code: 'occupancy:view', name: '查看入住', group: 'occupancy' },
  { code: 'occupancy:checkin', name: '办理入住', group: 'occupancy' },
  { code: 'occupancy:checkout', name: '办理退宿', group: 'occupancy' },
  { code: 'occupancy:transfer', name: '调换宿舍', group: 'occupancy' },
  { code: 'occupancy:delete', name: '归档入住记录', group: 'occupancy' },
  { code: 'expense:view', name: '查看费用', group: 'expense' },
  { code: 'expense:create', name: '录入费用', group: 'expense' },
  { code: 'expense:edit', name: '编辑费用', group: 'expense' },
  { code: 'expense:delete', name: '归档费用', group: 'expense' },
  { code: 'bill:view', name: '查看账单', group: 'bill' },
  { code: 'bill:generate', name: '生成账单', group: 'bill' },
  { code: 'bill:confirm', name: '确认账单', group: 'bill' },
  { code: 'bill:delete', name: '归档账单', group: 'bill' },
  { code: 'bill:export-excel', name: '导出 Excel', group: 'bill' },
  { code: 'bill:export-pdf', name: '导出 PDF', group: 'bill' },
  { code: 'deposit:view', name: '查看押金', group: 'deposit' },
  { code: 'deposit:create', name: '新增押金', group: 'deposit' },
  { code: 'deposit:edit', name: '编辑押金', group: 'deposit' },
  { code: 'deposit:delete', name: '归档押金', group: 'deposit' },
  { code: 'deposit:refund', name: '直接退还押金', group: 'deposit' },
  { code: 'wallet:view', name: '查看学生余额', group: 'wallet' },
  { code: 'wallet:edit', name: '充值和调账', group: 'wallet' },
  { code: 'classroom:view', name: '查看教室', group: 'classroom' },
  { code: 'classroom:create', name: '新增教室', group: 'classroom' },
  { code: 'classroom:edit', name: '编辑教室', group: 'classroom' },
  { code: 'classroom:delete', name: '归档教室', group: 'classroom' },
  { code: 'organization:view', name: '查看机构', group: 'organization' },
  { code: 'organization:create', name: '新增机构', group: 'organization' },
  { code: 'organization:edit', name: '编辑机构', group: 'organization' },
  { code: 'organization:delete', name: '归档机构', group: 'organization' },
  { code: 'rental:view', name: '查看租赁订单', group: 'rental' },
  { code: 'rental:create', name: '新增租赁订单', group: 'rental' },
  { code: 'rental:edit', name: '编辑租赁订单', group: 'rental' },
  { code: 'rental:delete', name: '归档租赁订单', group: 'rental' },
  { code: 'log:view', name: '查看操作日志', group: 'log' },
  { code: 'log:create', name: '写入操作日志', group: 'log' },
  { code: 'user:view', name: '查看用户', group: 'user' },
  { code: 'user:create', name: '创建用户', group: 'user' },
  { code: 'user:edit', name: '编辑用户', group: 'user' },
  { code: 'user:reset-password', name: '重置密码', group: 'user' },
  { code: 'role:view', name: '查看角色', group: 'role' },
  { code: 'role:create', name: '创建角色', group: 'role' },
  { code: 'role:edit', name: '编辑角色', group: 'role' },
  { code: 'role:delete', name: '停用角色', group: 'role' },
  { code: 'class:view', name: '查看班级', group: 'class' },
  { code: 'class:create', name: '创建班级', group: 'class' },
  { code: 'class:edit', name: '编辑班级', group: 'class' },
  { code: 'class:delete', name: '归档班级', group: 'class' },
  { code: 'schedule:view', name: '查看排课', group: 'schedule' },
  { code: 'schedule:create', name: '创建排课', group: 'schedule' },
  { code: 'schedule:edit', name: '编辑排课', group: 'schedule' },
  { code: 'schedule:delete', name: '停用排课', group: 'schedule' },
  { code: 'attendance:view', name: '查看考勤', group: 'attendance' },
  { code: 'attendance:create', name: '新增考勤', group: 'attendance' },
  { code: 'attendance:edit', name: '编辑全部考勤', group: 'attendance' },
  { code: 'attendance:self-edit', name: '编辑任教班级考勤', group: 'attendance-scope' },
  { code: 'attendance:export', name: '导出考勤', group: 'attendance' },
  { code: 'sync:trigger', name: '触发数据同步', group: 'sync' },
  { code: 'sync:read', name: '查看同步状态', group: 'sync' },
  { code: 'integration:trigger', name: '触发集成', group: 'integration' },
  { code: 'integration:read', name: '查看集成状态', group: 'integration' },
  // 永久删除（两步删除：先归档/取消，再在已归档视图物理删除）
  { code: 'student:purge', name: '永久删除学生', group: 'purge' },
  { code: 'room:purge', name: '永久删除宿舍', group: 'purge' },
  { code: 'classroom:purge', name: '永久删除教室', group: 'purge' },
  { code: 'occupancy:purge', name: '永久删除入住记录', group: 'purge' },
  { code: 'expense:purge', name: '永久删除费用', group: 'purge' },
  { code: 'exam:purge', name: '永久删除考试', group: 'purge' },
  { code: 'bill:purge', name: '永久删除账单', group: 'purge' },
  { code: 'deposit:purge', name: '永久删除押金', group: 'purge' },
  { code: 'organization:purge', name: '永久删除机构', group: 'purge' },
  { code: 'rental:purge', name: '永久删除租赁订单', group: 'purge' },
  { code: 'class:purge', name: '永久删除班级', group: 'purge' },
  { code: 'user:purge', name: '永久删除用户', group: 'purge' },
  { code: 'archive:purge', name: '永久删除档案记录', group: 'purge' },
  { code: 'ai:config:read', name: '查看 AI 配置', group: 'ai' },
  { code: 'ai:config:write', name: '修改 AI 配置', group: 'ai' },
  { code: 'ai:config:test', name: '测试 AI 连接', group: 'ai' },
  { code: 'ai:chat:use', name: '使用 AI 助手', group: 'ai-chat' },
];

export const DEPRECATED_PERMISSION_CODES = [
  'profile:view',
  'attendance:generate',
  'learning:create',
  'learning:edit',
  'learning:delete',
  'exam:create',
  'exam:edit',
  'exam:delete',
  'department:view',
  'department:edit',
  'department:delete',
  // Legacy permission codes from older admin UI / seed data.
  'student:add',
  'student:update',
  'room:add',
  'room:update',
  'occupancy:add',
  'occupancy:update',
  'attendance:add',
  'attendance:update',
  'attendance:delete',
  'attendance:batch',
  'bill:export',
  'deposit:collect',
  'expense:add',
  'expense:update',
  'class:add',
  'class:update',
  'schedule:add',
  'schedule:update',
  'classroom:add',
  'classroom:update',
  'rental:add',
  'rental:update',
  'role:add',
  'role:update',
  'user:add',
  'user:update',
  'archive:view',
  'archive:import',
  'archive:export',
  'report:generate',
] as const;

export const DEPRECATED_PERMISSION_CODE_SET = new Set<string>(DEPRECATED_PERMISSION_CODES);

export function getChinaDateParts(date = new Date()): { date: string; weekDay: number } {
  const parts = Object.fromEntries(
    new Intl.DateTimeFormat('en-CA', {
      timeZone: 'Asia/Shanghai',
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      weekday: 'short',
    })
      .formatToParts(date)
      .filter((part) => part.type !== 'literal')
      .map((part) => [part.type, part.value]),
  );
  const weekDays: Record<string, number> = {
    Mon: 1,
    Tue: 2,
    Wed: 3,
    Thu: 4,
    Fri: 5,
    Sat: 6,
    Sun: 7,
  };
  return {
    date: `${parts.year}-${parts.month}-${parts.day}`,
    weekDay: weekDays[parts.weekday],
  };
}

export const PRESET_ROLES: Array<{
  name: string;
  code: string;
  description: string;
  isSystem: boolean;
  permissionGroups: string[];
  extraPermissions?: string[];
  legacyNames?: string[];
  legacyCodes?: string[];
}> = [
  {
    name: '超级管理员',
    code: 'super_admin',
    description: '系统初始化、应急维护和全局权限处理',
    isSystem: true,
    permissionGroups: [],
    legacyNames: ['超管', 'super_admin'],
  },
  {
    name: '任课老师',
    code: 'teacher',
    description: '查看自己的排课、今日课程和任教班级考勤',
    isSystem: true,
    permissionGroups: ['notification'],
    extraPermissions: [
      'teacher-workspace:view',
      'schedule:view',
      'attendance:view',
      'attendance:create',
      'attendance:self-edit',
    ],
    legacyNames: ['老师'],
  },
  {
    name: '教务管理员',
    code: 'academic',
    description: '管理学生、班级、教师、全局排课和历史考勤',
    isSystem: true,
    permissionGroups: [
      'student',
      'exam',
      'class',
      'schedule',
      'attendance',
      'classroom',
      'dashboard',
      'notification',
    ],
    extraPermissions: [
      'teacher-workspace:view',
      'teacher:view',
      'teacher:edit',
      'sync:read',
      'sync:trigger',
    ],
    legacyNames: ['教务'],
  },
  {
    name: '住宿运营管理员',
    code: 'accommodation_operations',
    description: '管理宿舍、入住、住宿费用、账单、押金和退宿结算',
    isSystem: true,
    permissionGroups: [
      'room',
      'occupancy',
      'expense',
      'bill',
      'deposit',
      'wallet',
      'dashboard',
      'notification',
    ],
    extraPermissions: ['student:basic-view'],
    legacyNames: ['宿管老师', '宿管', '财务'],
    legacyCodes: ['dormitory_supervisor', 'dorm_manager', 'finance'],
  },
  {
    name: '教室运营管理员',
    code: 'classroom_operations',
    description: '管理教室、教室排期、外部机构和租赁订单',
    isSystem: true,
    permissionGroups: ['classroom', 'rental', 'organization', 'notification'],
    legacyNames: ['机构负责人'],
    legacyCodes: ['institution_head'],
  },
  {
    name: '系统管理员',
    code: 'system_admin',
    description: '管理账号、角色、日志、同步和系统配置',
    isSystem: true,
    permissionGroups: ['user', 'role', 'log', 'integration', 'sync', 'ai', 'notification'],
  },
];
