import * as ExcelJS from 'exceljs';
import {
  createStudentImportTemplateWorkbook,
  parseStudentImportWorkbook,
} from './student-import';

describe('student import workbook', () => {
  it('creates a template with one main example row and header-only detail sheets', () => {
    const workbook = createStudentImportTemplateWorkbook();
    const students = workbook.getWorksheet('学生基础+档案+录取');

    expect(workbook.worksheets.map((sheet) => sheet.name)).toEqual([
      '学生基础+档案+录取',
      '报读班型',
      '考试成绩',
      '课堂回访',
    ]);
    expect(students?.rowCount).toBe(2);
    const headerRow = students?.getRow(1);
    const columnByHeader = new Map<string, number>();
    headerRow?.eachCell((cell, colNumber) => columnByHeader.set(String(cell.value), colNumber));
    expect(students?.getRow(2).getCell(1).value).toBe('13800138000');
    expect(students?.getRow(2).getCell(columnByHeader.get('大专院校') || 0).value).toBe(
      '北京职业技术学院',
    );
    expect(students?.getRow(2).getCell(columnByHeader.get('大专专业') || 0).value).toBe(
      '软件技术',
    );
    expect(students?.getRow(2).getCell(columnByHeader.get('录取状态') || 0).value).toBe('pending');
    expect(students?.getRow(2).getCell(columnByHeader.get('录取院校') || 0).value || '').toBe('');
    expect(students?.getRow(2).getCell(columnByHeader.get('录取专业') || 0).value || '').toBe('');
    expect(workbook.getWorksheet('报读班型')?.rowCount).toBe(1);
    expect(workbook.getWorksheet('考试成绩')?.rowCount).toBe(1);
    expect(workbook.getWorksheet('课堂回访')?.rowCount).toBe(1);
  });

  it('maps rows by headers across archive sheets', () => {
    const workbook = new ExcelJS.Workbook();
    const students = workbook.addWorksheet('学生基础+档案+录取');
    students.addRow([
      '手机号*',
      '姓名*',
      '目标院校',
      '大专院校',
      '大专专业',
      '建档日期',
      '文化课最终分',
      '录取状态',
    ]);
    students.addRow([
      '13800138000',
      '张三',
      '北京大学',
      '北京职业技术学院',
      '软件技术',
      '2024/9/1',
      620,
      'pending',
    ]);

    const enrollments = workbook.addWorksheet('报读班型');
    enrollments.addRow(['手机号*', '课程类别*', '班型*', '班级名称']);
    enrollments.addRow(['13800138000', 'culture', 'one_on_one', '冲刺班']);

    const examScores = workbook.addWorksheet('考试成绩');
    examScores.addRow(['手机号*', '考试类型*', '科目*', '成绩*', '考试日期']);
    examScores.addRow(['13800138000', 'monthly', '语文', '108.5', '2024-10-15']);

    const learningRecords = workbook.addWorksheet('课堂回访');
    learningRecords.addRow(['手机号*', '记录日期*', '记录类型*', '内容*']);
    learningRecords.addRow(['13800138000', '2024-10-20', 'study_feedback', '状态稳定']);

    const parsed = parseStudentImportWorkbook(workbook);

    expect(parsed.students[0]).toEqual(
      expect.objectContaining({
        phone: '13800138000',
        name: '张三',
        targetCollege: '北京大学',
        collegeSchool: '北京职业技术学院',
        collegeMajor: '软件技术',
        profileDate: '2024-09-01',
        cultureFinalScore: 620,
        admissionStatus: 'pending',
      }),
    );
    expect(parsed.enrollments[0]).toEqual(
      expect.objectContaining({ courseCategory: 'culture', classType: 'one_on_one' }),
    );
    expect(parsed.examScores[0]).toEqual(
      expect.objectContaining({ score: 108.5, examDate: '2024-10-15' }),
    );
    expect(parsed.learningRecords[0]).toEqual(
      expect.objectContaining({ recordType: 'study_feedback', content: '状态稳定' }),
    );
  });
});
