export type JsonSeedValue =
  | string
  | number
  | boolean
  | null
  | JsonSeedValue[]
  | { [key: string]: JsonSeedValue };

export interface AppDataSeedEntry {
  key: string;
  value: JsonSeedValue;
}

export const DEFAULT_PRODUCT_ID = 'product-ftb-app';
export const DEFAULT_PRODUCT_NAME = '翻台宝应用';
const seedDate = '2026-07-01';
const seedCreatedAt = '2026-07-01T00:00:00.000Z';

const importedProjectNames = [
  '通知公告',
  '产品库',
  'AI帮助',
  '读书',
  '工作计划',
  '考勤',
  '埋点统计',
  '培训',
  '案例学习',
  '权限管理',
  '人事评价',
  '数据分析',
  '薪酬',
  '巡店',
  '中差评',
  '值班',
  '我的',
  '通讯录',
  '人事入转调离',
  '审批',
  'IM即时聊天',
  '全局任务',
  '经营报告',
  '人才发展',
  '电子合同',
  '系统管理',
  '证件管理',
  '问卷调查',
  '充值',
  '企业微信机器人',
  '组织架构',
  '日志',
  '文化打卡',
  '资料库',
  '利润管理',
  '其它',
  '桌访',
  'AI舆情助手',
  'AI员工关系助手',
  'AI经营助手',
  '文件列表',
  '设备管理',
  '录音详情',
  '硬件对接',
  '词库配置',
  '数据同步',
  '算法服务',
  '硬件管理后台',
  '翻台宝后台',
  '评分面板',
  '桌访整合',
  '系统整合',
  '绩效',
  '日常助手',
  '店长助手',
  '增长管理',
];

export function buildDefaultProjects() {
  return importedProjectNames.map((name, index) => ({
    id: `project-ftb-${String(index + 1).padStart(3, '0')}`,
    name,
    description: '',
    createdAt: seedCreatedAt,
  }));
}

const customerSourceTargetNames = [
  '方砖厂',
  '全部客户',
  '晓秧锅',
  '老船长',
  '失眠烧烤',
  '百年神厨',
  '渝是乎',
  '王大爷烧烤',
  '左庭右院',
  '咱妈烀饼',
  '叁宝烤肉',
  '潼厨',
  '苏福记',
  '农耕记',
  '呼啦面馆',
  '古家辣子',
  '老郑家',
  '管氏',
  '六月雪',
  '公司内部',
  '天山传奇',
  '蜀镇',
  '灶东家',
  '牧家厨房',
  '大河宴',
  '蓉和妈妈菜',
  '记忆包工坊',
  '聚龙',
  '茅溪',
  '八菜一汤',
  '醉巴鲜',
  '菡湘餐饮',
  '洞庭楚湘',
  '建国烧烤',
  '吾里家',
  '蔚蓝书吧',
  '新沙烧烤',
  '酒煮江湖',
  '奎屯俭寨',
  '渝厨',
  '饼司令',
  '重庆面包路',
  '101披萨',
  '仲和小馆',
  '淄博黎叔',
  '秦阿哥',
  '秦门西安小馆',
  '西椒和牛',
  '渔痴鱼醉',
  '德天肥牛',
  '稻香园',
  '我叫湘',
  '贝勒爷南门涮肉',
  '丝恋丝娃娃',
  '赣掌门',
  '肖记牛肉鱼杂馆',
  '朱三伯',
  '西安70大院',
  '武圣羊汤',
  '壶茶馆',
  '熊家无二',
  '滇芸山',
  '壹周餐餐饮',
  '钟家凉粉',
  '炭亲干锅',
  '麻步姐',
  '杨记兴',
];

function buildCustomerSourceTargets(): JsonSeedValue[] {
  return customerSourceTargetNames.map((name, index) => ({
    id: `source-customer-${String(index + 1).padStart(3, '0')}`,
    name,
    sourceType: 'customer',
    createdAt: seedDate,
  }));
}

function buildDefaultProductOverview(): JsonSeedValue {
  const projects = buildDefaultProjects();
  return {
    id: DEFAULT_PRODUCT_ID,
    name: DEFAULT_PRODUCT_NAME,
    description: '',
    createdAt: seedCreatedAt,
    updatedAt: seedCreatedAt,
    projects,
    versions: [],
    _count: {
      requirements: 0,
      projects: projects.length,
      versions: 0,
    },
  };
}

const requirementTypes: JsonSeedValue[] = [
  { id: 'type-1', name: '新功能', createdAt: '2024-01-01' },
  { id: 'type-2', name: 'BUG修复', createdAt: '2024-01-01' },
  { id: 'type-3', name: '数据需求/埋点需求', createdAt: '2024-01-01' },
  { id: 'type-4', name: '优化需求', createdAt: '2024-01-01' },
  { id: 'type-5', name: '模块重构', createdAt: '2024-01-01' },
  { id: 'type-6', name: '缺陷修复', createdAt: '2024-01-01' },
  { id: 'type-7', name: '功能优化', createdAt: '2024-01-01' },
  { id: 'type-8', name: '插单/紧急需求', createdAt: '2024-01-01' },
];

const requirementPlatforms: JsonSeedValue[] = [
  { id: 'platform-1', name: 'Web', createdAt: '2024-01-01' },
  { id: 'platform-2', name: 'App(iOS)', createdAt: '2024-01-01' },
  { id: 'platform-3', name: 'App(Android)', createdAt: '2024-01-01' },
  { id: 'platform-4', name: '小程序', createdAt: '2024-01-01' },
  { id: 'platform-5', name: 'H5', createdAt: '2024-01-01' },
];

const overtimeReasons: JsonSeedValue[] = [
  { id: 'reason-1', name: '需求变更', createdAt: '2024-01-01' },
  { id: 'reason-2', name: '需求新增', createdAt: '2024-01-01' },
  { id: 'reason-3', name: '版本冲刺', createdAt: '2024-01-01' },
  { id: 'reason-4', name: 'Bug修复', createdAt: '2024-01-01' },
  { id: 'reason-5', name: '线上问题修复', createdAt: '2024-01-01' },
  { id: 'reason-6', name: '测试问题整改', createdAt: '2024-01-01' },
  { id: 'reason-7', name: '技术难点攻关', createdAt: '2024-01-01' },
  { id: 'reason-8', name: '联调问题', createdAt: '2024-01-01' },
  { id: 'reason-9', name: '上游交付延误', createdAt: '2024-01-01' },
  { id: 'reason-10', name: '工作量超负荷', createdAt: '2024-01-01' },
  { id: 'reason-11', name: '返工修改', createdAt: '2024-01-01' },
];

const departments: JsonSeedValue[] = [
  { id: 'dept-1', name: '产品部', order: 1, createdAt: '2024-01-01' },
  { id: 'dept-2', name: '技术部', order: 2, createdAt: '2024-01-01' },
  { id: 'dept-2-1', name: '前端组', order: 1, parentId: 'dept-2', createdAt: '2024-01-01' },
  { id: 'dept-2-2', name: '后端组', order: 2, parentId: 'dept-2', createdAt: '2024-01-01' },
  { id: 'dept-2-3', name: '测试组', order: 3, parentId: 'dept-2', createdAt: '2024-01-01' },
  { id: 'dept-3', name: '设计部', order: 3, createdAt: '2024-01-01' },
  { id: 'dept-4', name: '运营部', order: 4, createdAt: '2024-01-01' },
];

const roles: JsonSeedValue[] = [
  {
    id: 'role-admin',
    name: '超级管理员',
    isSystem: true,
    createdAt: '2024-01-01',
    description: '拥有系统全部权限',
    permissions: ['*'],
  },
  {
    id: 'role-pm',
    name: '产品经理',
    createdAt: '2024-01-01',
    description: '管理产品和需求',
    permissions: [
      'product:view',
      'product:create',
      'product:edit',
      'product:delete',
      'project:view',
      'project:create',
      'project:edit',
      'project:delete',
      'version:view',
      'version:create',
      'version:edit',
      'version:delete',
      'requirement:view',
      'requirement:create',
      'requirement:edit',
      'requirement:delete',
      'version.req:view',
      'version.req:manage',
      'version.product_plan:view',
      'version.product_plan:manage',
      'overtime:view',
      'member:view',
      'role:view',
      'version.research:view',
      'version.ui_plan:view',
      'version.devtask:view',
      'version.testcase:view',
      'version.bug:view',
      'xiaobao.warning:view',
      'xiaobao.warning:manage',
    ],
  },
  {
    id: 'role-lead',
    name: '组长',
    createdAt: '2024-01-01',
    description: '管理本组任务和本部门加班记录',
    permissions: [
      'product:view',
      'project:view',
      'version:view',
      'requirement:view',
      'version.req:view',
      'version.research:view',
      'version.product_plan:view',
      'version.ui_plan:view',
      'version.devtask:view',
      'version.devtask:manage',
      'version.testcase:view',
      'version.testcase:manage',
      'version.bug:view',
      'version.bug:create',
      'version.bug:edit',
      'version.bug:delete',
      'xiaobao.warning:view',
      'overtime:view',
      'overtime:create',
      'overtime:export',
    ],
  },
  {
    id: 'role-dev',
    name: '开发工程师',
    createdAt: '2024-01-01',
    description: '负责开发任务',
    permissions: [
      'product:view',
      'project:view',
      'version:view',
      'requirement:view',
      'version.req:view',
      'version.devtask:view',
      'version.devtask:manage',
      'version.bug:view',
      'version.bug:edit',
      'version.research:view',
      'version.product_plan:view',
      'version.ui_plan:view',
      'version.testcase:view',
      'overtime:view',
      'overtime:create',
      'xiaobao.warning:view',
    ],
  },
  {
    id: 'role-test',
    name: '测试工程师',
    createdAt: '2024-01-01',
    description: '负责测试任务',
    permissions: [
      'product:view',
      'project:view',
      'version:view',
      'requirement:view',
      'version.req:view',
      'version.testcase:view',
      'version.testcase:manage',
      'version.bug:view',
      'version.bug:create',
      'version.bug:edit',
      'version.bug:delete',
      'version.research:view',
      'version.product_plan:view',
      'version.ui_plan:view',
      'version.devtask:view',
      'overtime:view',
      'overtime:create',
      'xiaobao.warning:view',
    ],
  },
  {
    id: 'role-design',
    name: '设计师',
    createdAt: '2024-01-01',
    description: '负责UI/UX设计',
    permissions: [
      'product:view',
      'project:view',
      'version:view',
      'requirement:view',
      'version.req:view',
      'version.ui_plan:view',
      'version.ui_plan:manage',
      'version.research:view',
      'version.product_plan:view',
      'version.devtask:view',
      'version.testcase:view',
      'version.bug:view',
      'overtime:view',
      'overtime:create',
      'xiaobao.warning:view',
    ],
  },
];

const superAdminMember: JsonSeedValue = {
  id: 'm-8',
  name: '超级管理员',
  username: 'admin',
  email: '',
  phone: '',
  roleId: 'role-admin',
  isSystem: true,
  password: 'Ftb@2024',
  createdAt: '2024-01-01',
  departmentId: '',
};

const passwordRule: JsonSeedValue = {
  length: 8,
  prefix: 'Ftb',
  includeNumbers: true,
  includeSpecial: false,
  includeLowercase: true,
  includeUppercase: true,
};

export function buildAppDataSeed(): AppDataSeedEntry[] {
  return [
    { key: 'products-overview', value: [buildDefaultProductOverview()] },
    {
      key: 'requirements',
      value: {
        requirements: [],
        sourceTargets: buildCustomerSourceTargets(),
        types: requirementTypes,
        platforms: requirementPlatforms,
      },
    },
    { key: 'version-plans', value: [] },
    { key: 'dev-tasks', value: [] },
    { key: 'test-cases', value: [] },
    { key: 'bugs', value: [] },
    {
      key: 'members',
      value: {
        departments,
        members: [superAdminMember],
        roles,
        passwordRule,
      },
    },
    { key: 'task-categories', value: [] },
    { key: 'task-worklogs', value: [] },
    { key: 'work-activities', value: [] },
    { key: 'xiaobao-risk-insights', value: [] },
    { key: 'xiaobao-risk-snapshots', value: [] },
    { key: 'xiaobao-warning-views', value: [] },
    {
      key: 'overtime',
      value: {
        records: [],
        reasons: overtimeReasons,
      },
    },
  ];
}
