import { Student } from '../entities/student.entity';
import { StudentProfile } from '../entities/student-profile.entity';
import { StudentEnrollment } from '../entities/student-enrollment.entity';
import { esc, sectionFrame, sectionHeader, coverFooter } from './archive-report.helpers';
import { buildEnrollmentSection } from './archive-report.enrollment';

export function buildCover(
  student: Student,
  profile: StudentProfile | null,
  enrollments: StudentEnrollment[],
  now: string,
  tocNames: string[],
): string {
  const types = enrollments.map((e) => e.classType).filter(Boolean).join(' / ') || '-';

  const tocHtml =
    tocNames.length > 0
      ? tocNames
          .map(
            (name, i) => `
        <div class="toc-row"><span class="toc-index">${String(i + 1).padStart(2, '0')}</span><span class="toc-name">${esc(name)}</span></div>`,
          )
          .join('')
      : '<div class="toc-row"><span class="toc-name muted">暂无章节</span></div>';

  return sectionFrame(`
      ${sectionHeader('封面')}
      <div class="cover-title">学生档案报告</div>
      <div class="cover-subtitle">生成日期: ${esc(now)}</div>
      <div class="cover-main">
        <div class="cover-name-card">
          <div class="cover-name">${esc(student.name)}</div>
          <div class="cover-desc">学号: ${esc(student.studentNo || '-')}<br>身份证号: ${esc(student.idNumber || '-')}</div>
        </div>
        <div class="cover-info">
          <div class="cover-cell">
            <div class="label">科类方向</div>
            <div class="value">${esc(profile?.subjectDirection || '-')}</div>
          </div>
          <div class="cover-cell">
            <div class="label">目标院校</div>
            <div class="value">${esc(profile?.targetCollege || '-')}</div>
          </div>
          <div class="cover-cell">
            <div class="label">目标专业</div>
            <div class="value">${esc(profile?.targetMajor || '-')}</div>
          </div>
          <div class="cover-cell">
            <div class="label">报读班型</div>
            <div class="value">${esc(types)}</div>
          </div>
        </div>
      </div>
      <div class="toc">${tocHtml}</div>
      <div class="watermark">恭学教育</div>
      ${coverFooter()}
    `, true);
}

export function buildBasicInfo(
  student: Student,
  profile: StudentProfile | null,
  enrollments: StudentEnrollment[],
  now: string,
): string {
  const infoCards = `
      <div class="title-row">
        <div>
          <div class="source">${esc(now)} · 系统生成</div>
          <div class="section-title">基础信息</div>
        </div>
      </div>
      <div class="card" style="margin-bottom:14px;">
        <h3>个人信息</h3>
        <div class="grid-2">
          <div class="summary-row"><span>姓名</span><span><strong>${esc(student.name)}</strong></span></div>
          <div class="summary-row"><span>性别</span><span>${esc(student.gender || '-')}</span></div>
          <div class="summary-row"><span>电话</span><span>${esc(student.phone || '-')}</span></div>
          <div class="summary-row"><span>民族</span><span>${esc(student.ethnicity || '-')}</span></div>
          <div class="summary-row"><span>紧急联系人</span><span>${esc(student.emergencyContact || '-')}</span></div>
          <div class="summary-row"><span>紧急电话</span><span>${esc(student.emergencyPhone || '-')}</span></div>
          <div class="summary-row"><span>年级</span><span>${esc(profile?.grade || '-')}</span></div>
        </div>
      </div>`;

  const enrollmentSection = buildEnrollmentSection(enrollments);

  return sectionFrame(`
      ${sectionHeader('基础信息')}
      ${infoCards}
      ${enrollmentSection}
    `);
}
