/// <reference types="node" />

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

const count = Number(process.argv[2] || 1000);
const output = process.argv[3] || `student-import-${count}.xlsx`;

function getWorksheetOrThrow(workbook: ExcelJS.Workbook, name: string) {
  const sheet = workbook.getWorksheet(name);
  if (!sheet) throw new Error(`Worksheet not found: ${name}`);
  return sheet;
}

async function main() {
  if (!Number.isInteger(count) || count <= 0) {
    throw new Error('Count must be a positive integer');
  }

  const workbook = createStudentImportTemplateWorkbook();
  const students = getWorksheetOrThrow(workbook, '学生基础+档案+录取');
  const enrollments = getWorksheetOrThrow(workbook, '报读班型');
  const examScores = getWorksheetOrThrow(workbook, '考试成绩');
  const learningRecords = getWorksheetOrThrow(workbook, '课堂回访');

  for (let i = 1; i <= count; i++) {
    const serial = String(i).padStart(4, '0');
    const phone = `138${String(i).padStart(8, '0')}`;
    students.addRow({
      phone,
      name: `导入学生${serial}`,
      studentNo: `GX${serial}`,
      gender: i % 2 === 0 ? '女' : '男',
      idNumber: `110101200601${String((i % 28) + 1).padStart(2, '0')}${String(i % 1000).padStart(3, '0')}X`,
      ethnicity: '汉族',
      emergencyContact: `联系人${serial}`,
      emergencyPhone: `139${String(i).padStart(8, '0')}`,
      organization: '',
      supervisor: '',
      targetCollege: `目标院校${(i % 20) + 1}`,
      targetMajor: `目标专业${(i % 10) + 1}`,
      collegeSchool: `大专院校${(i % 12) + 1}`,
      collegeMajor: `大专专业${(i % 8) + 1}`,
      subjectDirection: ['物化生', '物化地', '史政地'][i % 3],
      grade: '高三',
      profileDate: '2024-09-01',
      notes: '',
      cultureFinalScore: 500 + (i % 151),
      professionalFinalScore: 300 + (i % 101),
      admissionStatus: 'pending',
      admittedCollege: '',
      admittedMajor: '',
    });

    enrollments.addRow({
      phone,
      name: `导入学生${serial}`,
      courseCategory: i % 2 === 0 ? 'culture' : 'professional',
      classType: i % 3 === 0 ? 'small_group' : 'one_on_one',
      className: `导入测试班${(i % 10) + 1}`,
      headTeacher: '',
      subjectTeacher: '',
      startDate: '2024-09-01',
      endDate: '2025-06-01',
      status: 'active',
    });

    examScores.addRow({
      phone,
      name: `导入学生${serial}`,
      examType: 'monthly',
      examName: '导入测试月考',
      subject: '语文',
      score: 80 + (i % 41),
      classAvg: 90,
      rank: (i % 50) + 1,
      examDate: '2024-10-15',
      enrollmentName: `导入测试班${(i % 10) + 1}`,
    });

    learningRecords.addRow({
      phone,
      name: `导入学生${serial}`,
      recordDate: '2024-10-20',
      recordType: 'study_feedback',
      content: `导入测试回访${serial}`,
      followUpMethod: 'phone',
      nextStep: '',
    });
  }

  for (const sheet of workbook.worksheets) {
    sheet.views = [{ state: 'frozen', ySplit: 1 }];
  }

  await workbook.xlsx.writeFile(output);
  console.log(`Generated ${count} students with ${STUDENT_IMPORT_COLUMNS.length} main columns: ${output}`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});
