export type IssuePriority = 'P0' | 'P1' | 'P2';
export type IssueStatus = 'open' | 'in_progress' | 'resolved' | 'pending_feedback';
export type TrackKey = 'product' | 'dev' | 'qa';

export interface FourRBlock {
  reason: string;
  measure: string;
  result: string;
  feedback: string;
}

export interface WeeklyIssue {
  id: string;
  priority: IssuePriority;
  status: IssueStatus;
  track: TrackKey;
  module: string;
  title: string;
  owner: string;
  updatedAt: string;
  fourR: FourRBlock;
}

export interface ProductRequirementItem {
  id: string;
  priority: IssuePriority | 'V1.0';
  title: string;
  note?: string;
  done: boolean;
}

export interface ProductRequirementGroup {
  key: string;
  label: string;
  items: ProductRequirementItem[];
}

export interface TrackLine {
  key: string;
  label: string;
  progress: number;
  planned: number;
  done: number;
  blocked: number;
  highlight: string;
}

export interface PhaseWorkItem {
  id: string;
  title: string;
  done: boolean;
  note?: string;
}

export type RequirementStage = '原型设计' | '需求确认' | '需求跟踪' | '需求规划' | '无需求开发';
export type RequirementPriority = '高优先级' | '中优先级' | '低优先级';

export interface RequirementItem {
  id: string;
  stage: RequirementStage;
  priority: RequirementPriority;
  title: string;
  progress: number;
}

export interface RequirementSection {
  title: string;
  items: RequirementItem[];
}

export interface PhaseTab {
  key: string;
  label: string;
  count: number;
  done: number;
  summary?: string;
  points?: string[];
  workReport?: PhaseWorkItem[];
  miniProgramWorkReport?: PhaseWorkItem[];
  miniProgramEmptyText?: string;
  emptyText?: string;
  requirementSections?: RequirementSection[];
}

export interface TrackPhase {
  key: string;
  label: string;
  count: number;
  done: number;
  note?: string;
  tabs?: PhaseTab[];
  workReportTitle?: string;
}

export interface TrackProgress {
  key: TrackKey;
  label: string;
  progress: number;
  planned: number;
  done: number;
  blocked: number;
  highlight?: string;
  lines?: TrackLine[];
  requirementGroups?: ProductRequirementGroup[];
  phases?: TrackPhase[];
}

export interface MilestoneRow {
  id: string;
  module: string;
  milestone: string;
  owner: string;
  planDate: string;
  actualDate: string | null;
  progress: number;
  status: 'on_track' | 'at_risk' | 'done' | 'delayed';
}

export interface WeeklyReportMeta {
  weekLabel: string;
  weekRange: string;
  reportDate: string;
  reporter: string;
  department: string;
}

export const REPORT_META: WeeklyReportMeta = {
  weekLabel: '2026 年第 25 周',
  weekRange: '2026-06-16 ~ 2026-06-22',
  reportDate: '2026-06-23',
  reporter: '数智部 · 产品组',
  department: '数智部',
};

export const TRACK_PROGRESS: TrackProgress[] = [
  {
    key: 'product',
    label: '产品',
    progress: 39,
    planned: 18,
    done: 7,
    blocked: 0,
    phases: [
      {
        key: 'plan',
        label: '规划',
        count: 5,
        done: 1,
        workReportTitle: '版本规划',
        tabs: [
          {
            key: 'v12',
            label: 'V1.2',
            count: 2,
            done: 0,
            workReport: [
              { id: 'WR-P-V12-1', title: 'V1.2版本需求管理矩阵', done: false },
              { id: 'WR-P-V12-2', title: 'V1.2版本WBS', done: false },
            ],
          },
          {
            key: 'v13',
            label: 'V1.3',
            count: 4,
            done: 0,
            workReport: [
              { id: 'WR-P-V13-P1', title: '资产链条全闭环', done: false },
              { id: 'WR-P-V13-P2', title: '合同合规性改造', done: false },
              { id: 'WR-P-V13-P3', title: '业财采运侧统计分析上线', done: false },
              { id: 'WR-P-V13-P4', title: '移动端商城发布', done: false },
            ],
          },
          {
            key: 'v14',
            label: 'V1.4',
            count: 3,
            done: 0,
            workReport: [
              { id: 'WR-P-V14-P1', title: '移动化', done: false },
              { id: 'WR-P-V14-P2', title: '外部用户业务引入', done: false },
              { id: 'WR-P-V14-P3', title: '业财一体化打通', done: false },
              { id: 'WR-P-V14-1', title: '在线商城需求预研', done: false },
            ],
          },
        ],
      },
      {
        key: 'interview',
        label: '访谈',
        count: 4,
        done: 2,
        workReportTitle: '访谈结果',
        tabs: [
          {
            key: 'biz',
            label: '业务',
            count: 1,
            done: 0,
            workReport: [
              { id: 'WR-I-BIZ-1', title: '部分业务部合同代提审人员权限确认', done: false },
            ],
          },
          {
            key: 'ops-mgmt',
            label: '业管',
            count: 3,
            done: 2,
            workReport: [
              { id: 'WR-I-OM-1', title: '租赁、自营台账需求逻辑确认和演示，进度反馈', done: true },
              { id: 'WR-I-OM-3', title: '在线合同部分业务逻辑确认和优化方向讨论', done: true },
              { id: 'WR-I-OM-4', title: '提车应收款的生成逻辑讨论和确认', done: false },
            ],
          },
          {
            key: 'procurement',
            label: '采购',
            count: 1,
            done: 1,
            workReport: [
              { id: 'WR-I-PR-2', title: '保险采购功能使用培训，用户实操反馈确认，优化意见采集', done: true },
            ],
          },
          {
            key: 'maint',
            label: '运维',
            count: 1,
            done: 0,
            workReport: [
              { id: 'WR-I-MT-1', title: '年审/保养台账原型演示和确认', done: false },
            ],
          },
          {
            key: 'finance',
            label: '财务',
            count: 0,
            done: 0,
            workReport: [],
            emptyText: '无',
          },
          {
            key: 'legal',
            label: '法务',
            count: 3,
            done: 3,
            workReport: [
              { id: 'WR-I-LG-1', title: '标准合同不同车型条款逻辑确认', done: true },
              { id: 'WR-I-LG-2', title: '标准合同里程约束和操作逻辑确认', done: true },
              { id: 'WR-I-LG-3', title: '标准合同多车型操作逻辑确认', done: true },
            ],
          },
          {
            key: 'ceo',
            label: 'CEO',
            count: 1,
            done: 0,
            summary: 'V1.0-V1.4 版本基调与 WBS 汇报',
            workReport: [{ id: 'WR-I-CEO-1', title: '版本基调口头对齐', done: true, note: 'WBS 文档整理中' }],
          },
        ],
      },
      {
        key: 'prototype',
        label: '需求',
        count: 20,
        done: 8,
        tabs: [
          {
            key: 'new-feature',
            label: '新增功能',
            count: 16,
            done: 6,
            requirementSections: [
              {
                title: 'PC',
                items: [
                  { id: 'WR-NF-W-1', stage: '原型设计', priority: '高优先级', title: '标准合同管理功能原型设计', progress: 90 },
                  { id: 'WR-NF-W-2', stage: '原型设计', priority: '高优先级', title: '新合同体系功能原型设计', progress: 70 },
                  { id: 'WR-NF-W-3', stage: '需求确认', priority: '高优先级', title: '客户评级体系功能及原型设计，有方案待与法务确认', progress: 25 },
                  { id: 'WR-NF-W-4', stage: '原型设计', priority: '高优先级', title: '维修保养台账功能，待确认', progress: 100 },
                  { id: 'WR-NF-W-5', stage: '原型设计', priority: '中优先级', title: '还车应结款运维处理人转交功能，未开始', progress: 0 },
                  { id: 'WR-NF-W-6', stage: '原型设计', priority: '高优先级', title: '合同里程减免功能，合并到新合同体系，现版本考虑手工', progress: 30 },
                  { id: 'WR-NF-W-7', stage: '原型设计', priority: '中优先级', title: '三方退租功能，择期评审', progress: 100 },
                  { id: 'WR-NF-W-8', stage: '需求跟踪', priority: '高优先级', title: '保险采购功能，已上线，部分体验优化已提', progress: 100 },
                  { id: 'WR-NF-W-9', stage: '需求跟踪', priority: '高优先级', title: '加氢站管理功能，已评审开发中', progress: 50 },
                  { id: 'WR-NF-W-10', stage: '原型设计', priority: '中优先级', title: '加氢站登录和加氢订单记录功能，等待加氢站管理上线推进', progress: 100 },
                  { id: 'WR-NF-W-11', stage: '原型设计', priority: '高优先级', title: '租赁台账功能，部分细节需求需要进一步确认', progress: 95 },
                  { id: 'WR-NF-W-12', stage: '原型设计', priority: '高优先级', title: '租赁业务盈亏表', progress: 80 },
                  { id: 'WR-NF-W-13', stage: '原型设计', priority: '高优先级', title: '自营业务盈亏表', progress: 40 },
                  { id: 'WR-NF-W-14', stage: '原型设计', priority: '中优先级', title: '总体业务盈亏表', progress: 40 },
                ],
              },
              {
                title: '小程序',
                items: [
                  { id: 'WR-NF-M-1', stage: '需求跟踪', priority: '中优先级', title: '审批中心「提车应收款、还车应结款」功能，测试发现部分问题，开发重新修改', progress: 65 },
                ],
              },
              {
                title: '小羚羚',
                items: [
                  { id: 'WR-NF-X-1', stage: '需求规划', priority: '高优先级', title: '外部用户注册登录、在线商城，依赖web新合同功能，有完整规划', progress: 20 },
                ],
              },
            ],
          },
          {
            key: 'optimize',
            label: '功能优化',
            count: 5,
            done: 1,
            requirementSections: [
              {
                title: 'PC',
                items: [
                  { id: 'WR-OP-W-1', stage: '需求确认', priority: '中优先级', title: '提车应收款功能、UI、交互全面优化，有方案待与业管确认', progress: 30 },
                  { id: 'WR-OP-W-2', stage: '原型设计', priority: '中优先级', title: '车辆氢费明细抹零、客户核对功能', progress: 0 },
                  { id: 'WR-OP-W-3', stage: '需求确认', priority: '低优先级', title: '运维特殊权限体系设计，待确认', progress: 20 },
                  { id: 'WR-OP-W-4', stage: '无需求开发', priority: '高优先级', title: '合同续签/转正式/转三方/新增车辆等逻辑修复规则确认，开发中', progress: 60 },
                ],
              },
              {
                title: '小程序',
                items: [
                  { id: 'WR-OP-M-1', stage: '需求跟踪', priority: '中优先级', title: '交车管理功能优化，测试中', progress: 85 },
                ],
              },
            ],
          },
        ],
      },
      {
        key: 'review',
        label: '评审',
        count: 4,
        done: 2,
        workReportTitle: '评审结果',
        tabs: [
          {
            key: 'reviewed',
            label: '已评审',
            count: 2,
            done: 2,
            workReport: [
              { id: 'WR-R-DONE-1', title: '「加氢站管理」站点管理/充值单/对账单/余额/营业状况/成本价维护评审完成', done: true },
              { id: 'WR-R-DONE-2', title: '「租赁合同」续签/转正式/转三方/新增车辆功能缺失逻辑评审确认', done: true },
            ],
          },
          {
            key: 'pending',
            label: '待评审',
            count: 2,
            done: 0,
            workReport: [
              { id: 'WR-R-PND-1', title: '「租赁台账」功能需求评审，待部分细节和实现逻辑与业管确认后评审', done: false },
              { id: 'WR-R-PND-2', title: '「自营台账」功能需求评审，部分依赖模块需求完善后评审，下周一评审', done: false },
            ],
          },
        ],
      },
    ],
  },
  {
    key: 'dev',
    label: '开发',
    progress: 72,
    planned: 11,
    done: 8,
    blocked: 2,
    lines: [
      {
        key: 'oneos',
        label: 'OneOS',
        progress: 78,
        planned: 7,
        done: 6,
        blocked: 1,
        highlight: '车辆管理联调完成 85%；保险采购模块已迁入 oneos1.2 原型库',
      },
      {
        key: 'bi',
        label: 'BI',
        progress: 64,
        planned: 4,
        done: 2,
        blocked: 1,
        highlight: '经营看板 v2 接口对接中；车辆油耗报表 SQL 优化待 DBA 评审',
      },
    ],
  },
  {
    key: 'qa',
    label: '测试',
    progress: 81,
    planned: 8,
    done: 6,
    blocked: 0,
    highlight: '租赁合同创建页 E2E 用例 12/15 通过；性能基线回归无新增 P0',
  },
];

export const KEY_ISSUES: WeeklyIssue[] = [
  {
    id: 'ISS-001',
    priority: 'P0',
    status: 'in_progress',
    track: 'dev',
    module: '租赁合同管理',
    title: '新增页左侧多卡片布局在窄屏下偶发错位',
    owner: '前端 · 王磊',
    updatedAt: '2026-06-21',
    fourR: {
      reason: 'Ant Design 5 Select 与 DatePicker 默认高度不一致，叠加 flex 嵌套导致子卡片被压缩为 0 高度。',
      measure: '统一 36px 控件高度链；左侧列改为纵向滚动 + 各卡片 height:auto；补充交付区与表格列专项样式。',
      result: '桌面端三卡片布局已对齐；表格内租金/保证金输入框与车牌号选择器高度一致。移动端仍需 992px 以下回归。',
      feedback: '待总监确认：是否接受移动端整页纵向滚动方案（不再左右分栏固定高度）。',
    },
  },
  {
    id: 'ISS-002',
    priority: 'P0',
    status: 'pending_feedback',
    track: 'qa',
    module: '车辆管理',
    title: '保险状态与保险采购台账偶发不一致',
    owner: '测试 · 陈悦',
    updatedAt: '2026-06-20',
    fourR: {
      reason: '车辆列表取 insurance-purchases.json 最新一条，详情页缓存未刷新；localStorage 脏数据未隔离。',
      measure: '统一 insurance.ts 聚合逻辑；详情 Tab 进入时强制 re-fetch；测试环境增加 storage 前缀隔离。',
      result: '复现路径已稳定；修复 PR #428 合并 staging，回归 48 台样例车全部一致。',
      feedback: '请总监确认：正式环境是否允许用户手动清缓存，或仅后台运维可触发全量重算。',
    },
  },
  {
    id: 'ISS-003',
    priority: 'P1',
    status: 'resolved',
    track: 'product',
    module: '保险采购',
    title: '比价单与保单台账业务边界需书面确认',
    owner: '产品 · 李敏',
    updatedAt: '2026-06-19',
    fourR: {
      reason: '业务方对「比价单审批中是否占用车辆险种」理解不一致，影响 KPI 统计口径。',
      measure: '输出双业务线流程图 + 状态机说明；与法务、运营联合评审 1 次。',
      result: '已发布《保险采购模块方案书》PDF v1.2；原型按「不关联」实现获业务签字。',
      feedback: '总监已批示「按方案书执行」，无追加意见。',
    },
  },
];

export const MINOR_ISSUES: WeeklyIssue[] = [
  {
    id: 'ISS-101',
    priority: 'P2',
    status: 'open',
    track: 'dev',
    module: '合同模板',
    title: '预览分页与编辑区分页测量偶差 1px',
    owner: '王磊',
    updatedAt: '2026-06-18',
    fourR: { reason: '—', measure: '—', result: '—', feedback: '—' },
  },
  {
    id: 'ISS-102',
    priority: 'P2',
    status: 'in_progress',
    track: 'qa',
    module: '全局',
    title: 'Safari 下 DatePicker 焦点环被裁剪',
    owner: '陈悦',
    updatedAt: '2026-06-17',
    fourR: { reason: '—', measure: '—', result: '—', feedback: '—' },
  },
  {
    id: 'ISS-103',
    priority: 'P2',
    status: 'resolved',
    track: 'product',
    module: '车辆管理',
    title: 'PRD 导出文件名编码在 Windows 乱码',
    owner: '李敏',
    updatedAt: '2026-06-16',
    fourR: { reason: '—', measure: '—', result: '—', feedback: '—' },
  },
  {
    id: 'ISS-104',
    priority: 'P2',
    status: 'open',
    track: 'dev',
    module: '租赁订单',
    title: '服务项 Modal 在 1280px 下横向滚动条样式不统一',
    owner: '赵航',
    updatedAt: '2026-06-15',
    fourR: { reason: '—', measure: '—', result: '—', feedback: '—' },
  },
  {
    id: 'ISS-105',
    priority: 'P2',
    status: 'in_progress',
    track: 'qa',
    module: '保险采购',
    title: 'OCR 识别任务 mock 进度条动画在 CI 不稳定',
    owner: '陈悦',
    updatedAt: '2026-06-14',
    fourR: { reason: '—', measure: '—', result: '—', feedback: '—' },
  },
];

export const MILESTONES: MilestoneRow[] = [
  {
    id: 'MS-1',
    module: '租赁合同管理',
    milestone: '新增合同 · 主体/订单/委托书分卡',
    owner: '王磊',
    planDate: '2026-06-20',
    actualDate: '2026-06-21',
    progress: 100,
    status: 'done',
  },
  {
    id: 'MS-2',
    module: '保险采购',
    milestone: '原型迁入 oneos1.2 + 构建通过',
    owner: '赵航',
    planDate: '2026-06-22',
    actualDate: '2026-06-22',
    progress: 100,
    status: 'done',
  },
  {
    id: 'MS-3',
    module: '车辆管理',
    milestone: '保险跳转保险采购联调',
    owner: '王磊',
    planDate: '2026-06-25',
    actualDate: null,
    progress: 65,
    status: 'at_risk',
  },
  {
    id: 'MS-4',
    module: '合同模板 V26.6',
    milestone: '预览与编辑分页一致性',
    owner: '王磊',
    planDate: '2026-06-28',
    actualDate: null,
    progress: 40,
    status: 'on_track',
  },
];

export function countIssuesByPriority(issues: WeeklyIssue[], priority: IssuePriority) {
  return issues.filter((i) => i.priority === priority).length;
}

export function countIssuesByStatus(issues: WeeklyIssue[], status: IssueStatus) {
  return issues.filter((i) => i.status === status).length;
}

export type ExecutiveHealth = 'green' | 'amber' | 'red';

export interface ExecutiveKpi {
  key: string;
  label: string;
  value: string;
  hint?: string;
  tone?: 'default' | 'primary' | 'warning' | 'success' | 'danger';
}

export interface ExecutiveSummary {
  health: ExecutiveHealth;
  healthLabel: string;
  headline: string;
  achievements: string[];
  risks: string[];
}

export interface ExecutiveDecision {
  id: string;
  title: string;
  impact: string;
  recommendation: string;
  deadline: string;
  urgency: 'high' | 'medium';
}

export interface NextWeekItem {
  id: string;
  type: 'goal' | 'risk';
  title: string;
  detail: string;
}

export const EXECUTIVE_KPIS: ExecutiveKpi[] = [
  {
    key: 'v12-progress',
    label: 'V1.2 版本整体进度',
    value: '64%',
    hint: '较上周 +4%',
    tone: 'primary',
  },
  {
    key: 'milestone',
    label: '里程碑按时达成',
    value: '2 / 4',
    hint: '本季节点 50%',
    tone: 'warning',
  },
  {
    key: 'delivery',
    label: '本周价值交付',
    value: '5 项',
    hint: '上线 1 · 评审 4',
    tone: 'success',
  },
  {
    key: 'decision',
    label: '待高层决策',
    value: '2 项',
    hint: '需在本周内闭环',
    tone: 'warning',
  },
  {
    key: 'blocker',
    label: '阻塞/高风险',
    value: '2 项',
    hint: '开发阻塞 2',
    tone: 'danger',
  },
];

export const EXECUTIVE_SUMMARY: ExecutiveSummary = {
  health: 'amber',
  healthLabel: '总体可控 · 局部需加速',
  headline:
    '本周 OneOS V1.2 建设按节奏推进：标准合同与新合同体系原型接近收尾，保险采购已上线运行，加氢站管理与租赁合同关键逻辑完成评审。租赁/自营台账进入评审前最后一轮需求确认，提车应收款口径仍待跨部门拍板。开发联调整体正常，车辆管理与合同模板各 1 项技术阻塞需资源协调。',
  achievements: [
    '保险采购功能正式上线，业管完成使用培训并收集优化意见',
    '加氢站管理、租赁合同续签/转正式等 4 项需求完成评审或规则确认',
    '跨部门访谈覆盖业务、业管、采购、法务等 7 条战线，需求矩阵与 WBS 框架明确',
  ],
  risks: [
    '租赁台账、自营台账评审时点依赖业管细节确认，存在挤压 V1.2 里程碑窗口风险',
    '提车应收款生成逻辑未最终确认，可能影响审批中心小程序联调与财务对账口径',
  ],
};

export const EXECUTIVE_DECISIONS: ExecutiveDecision[] = [
  {
    id: 'DEC-1',
    title: '租赁/自营台账正式评审排期',
    impact: '决定 V1.2 资产链条与台账能力能否按期进入开发与 UAT',
    recommendation: '建议责成业管本周内完成细节确认，下周一启动自营台账评审，租赁台账紧随其后',
    deadline: '2026-06-27',
    urgency: 'high',
  },
  {
    id: 'DEC-2',
    title: '提车应收款生成逻辑与财务口径',
    impact: '影响审批中心小程序、财务对账及后续业财一体化数据一致性',
    recommendation: '建议由董事长协调业管与财务联合拍板，明确规则后 3 日内冻结需求',
    deadline: '2026-06-25',
    urgency: 'high',
  },
  {
    id: 'DEC-3',
    title: '合同里程减免 V1.2 实现策略',
    impact: '决定本期是否投入研发，或维持手工过渡方案',
    recommendation: '建议确认「合并至新合同体系、本期手工处理」策略，避免重复建设',
    deadline: '2026-06-30',
    urgency: 'medium',
  },
];

export const NEXT_WEEK_PLAN: NextWeekItem[] = [
  {
    id: 'NW-1',
    type: 'goal',
    title: '自营台账需求评审',
    detail: '下周一组织正式评审，会前补齐依赖模块需求说明',
  },
  {
    id: 'NW-2',
    type: 'goal',
    title: 'V1.2 版本 WBS 与需求矩阵定稿',
    detail: '输出可执行 WBS，作为开发与测试排期基线',
  },
  {
    id: 'NW-3',
    type: 'goal',
    title: '车辆管理保险联调闭环',
    detail: '推动保险状态一致性修复上线，消除 P0 质量风险',
  },
  {
    id: 'NW-4',
    type: 'risk',
    title: 'BI 数据源迁移外部依赖',
    detail: '若数据平台排期延后，将直接影响 V1.2 统计分析上线节点',
  },
  {
    id: 'NW-5',
    type: 'risk',
    title: '开发人力被合同模板专项占用',
    detail: '分页一致性专项与租赁合同新增页问题并行，需评估是否临时增援',
  },
];
