import * as XLSX from 'xlsx';
import { LIST_PAGE_TEST_CASES, DETAIL_PAGE_TEST_CASES, type VehicleTestCase } from './testCasesData';

const TEST_CASE_HEADERS = [
  '用例编号',
  '类型',
  '功能模块',
  '用例名称',
  '前置条件',
  '测试步骤',
  '预期结果',
  '优先级',
] as const;

function caseToRow(testCase: VehicleTestCase): string[] {
  return [
    testCase.id,
    testCase.type,
    testCase.module,
    testCase.name,
    testCase.precondition,
    testCase.steps,
    testCase.expected,
    testCase.priority,
  ];
}

function buildSheet(testCases: VehicleTestCase[]) {
  return XLSX.utils.aoa_to_sheet([
    [...TEST_CASE_HEADERS],
    ...testCases.map(caseToRow),
  ]);
}

/** 导出列表页 + 详情页测试用例 Excel（两个 Sheet） */
export function downloadVehicleTestCasesExcel(onComplete?: (message: string) => void) {
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, buildSheet(LIST_PAGE_TEST_CASES), '列表页测试用例');
  XLSX.utils.book_append_sheet(workbook, buildSheet(DETAIL_PAGE_TEST_CASES), '详情页测试用例');
  XLSX.writeFile(workbook, '车辆管理-测试用例.xlsx');
  onComplete?.('已下载测试用例 Excel（含列表页、详情页两个 Sheet）');
}
