import {
  createOccupancyImportTemplateWorkbook,
  OCCUPANCY_IMPORT_COLUMNS,
  parseOccupancyImportWorksheet,
} from './occupancy-import-template';

describe('occupancy import template', () => {
  it('includes the current occupancy fields including separate student and ID numbers', () => {
    const workbook = createOccupancyImportTemplateWorkbook();
    const ws = workbook.getWorksheet('入住名单导入模板')!;
    const headers = ws.getRow(1).values as unknown[];

    expect(headers).toEqual(
      expect.arrayContaining([
        '宿舍号',
        '楼栋',
        '床位号',
        '柜子号',
        '计费起始日',
        '电话',
        '学号',
        '身份证号',
        '入住类型',
        '备注',
      ]),
    );
    expect(headers).not.toContain('所属机构');
    expect(headers).not.toContain('学号/身份证');
    expect(ws.columnCount).toBe(OCCUPANCY_IMPORT_COLUMNS.length);
  });

  it('documents the current phone matching, organization, and deposit behavior', () => {
    const workbook = createOccupancyImportTemplateWorkbook();
    const helpWs = workbook.getWorksheet('使用说明')!;
    const instructions = helpWs.getColumn(1).values.join('\n');

    expect(instructions).toContain('按手机号关联已有学生');
    expect(instructions).toContain('学号和身份证号为两个独立字段');
    expect(instructions).toContain('所属机构自动取学生档案');
    expect(instructions).toContain('导入时自动收押金');
    expect(instructions).toContain('历史入住不会自动收取');
  });

  it('keeps Excel Date cells on the same local calendar day', () => {
    const workbook = createOccupancyImportTemplateWorkbook();
    const ws = workbook.getWorksheet('入住名单导入模板')!;
    ws.getCell('K2').value = new Date(2026, 3, 21);
    ws.getCell('L2').value = new Date(2026, 3, 22);
    ws.getCell('M2').value = new Date(2026, 3, 30);

    expect(parseOccupancyImportWorksheet(ws)[0]).toMatchObject({
      checkInDate: '2026-04-21',
      billingStartDate: '2026-04-22',
      checkOutDate: '2026-04-30',
    });
  });

  it('parses rows by header so new columns do not shift existing fields', () => {
    const workbook = createOccupancyImportTemplateWorkbook();
    const ws = workbook.getWorksheet('入住名单导入模板')!;
    const rows = parseOccupancyImportWorksheet(ws);

    expect(rows[0]).toMatchObject({
      roomNumber: '4-102',
      building: '4号楼',
      bedNumber: '1号床',
      lockerNumber: 'A01',
      name: '张三',
      studentNo: '2024001',
      idNumber: '11010120060101001X',
      checkInDate: '2026-04-21',
      billingStartDate: '2026-04-21',
      stayType: 'short',
    });
    expect(rows[1]).toMatchObject({
      bedNumber: '2号床',
      lockerNumber: 'A02',
      studentNo: '2024002',
      idNumber: '11010120060202002X',
      stayType: 'long',
    });
  });
});
