import { LearningRecord } from '../entities/learning-record.entity';
import { ResultArchive } from '../entities/result-archive.entity';
import { esc, sectionFrame, sectionHeader } from './archive-report.helpers';

export function buildLearning(learnings: LearningRecord[], now: string): string {
  if (learnings.length === 0) return '';

  const latest = learnings.slice(0, 15);
  let rows = '';
  for (const r of latest) {
    rows += `<tr>
        <td class="nowrap">${esc(r.recordDate || '-')}</td>
        <td>${esc(r.recordType || '-')}</td>
        <td>${esc((r.content || '-').slice(0, 200))}</td>
        <td>${esc(r.followUpMethod || '-')}</td>
      </tr>`;
  }

  return sectionFrame(`
      ${sectionHeader('学情记录')}
      <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>
        <table class="data-table">
          <thead><tr>
            <th style="width:90px;">日期</th><th style="width:60px;">类型</th>
            <th>内容</th><th style="width:70px;">跟进方式</th>
          </tr></thead>
          <tbody>${rows}</tbody>
        </table>
      </div>
    `);
}

export function buildResult(result: ResultArchive | null, _now: string): string {
  if (!result) return '';

  return sectionFrame(`
      ${sectionHeader('录取归档')}
      <div class="title-row">
        <div>
          <div class="section-title">录取归档</div>
        </div>
      </div>
      <div class="card">
        <div class="grid-2">
          <div class="summary-row"><span>文化课成绩</span><span><strong>${result.cultureFinalScore != null ? result.cultureFinalScore : '-'}</strong></span></div>
          <div class="summary-row"><span>专业课成绩</span><span><strong>${result.professionalFinalScore != null ? result.professionalFinalScore : '-'}</strong></span></div>
          <div class="summary-row"><span>录取状态</span><span><strong>${esc(result.admissionStatus || '-')}</strong></span></div>
          <div class="summary-row"><span>录取院校</span><span><strong>${esc(result.admittedCollege || '-')}</strong></span></div>
          <div class="summary-row"><span>录取专业</span><span><strong>${esc(result.admittedMajor || '-')}</strong></span></div>
        </div>
      </div>
      <div class="note">录取归档信息为最终结果，如有疑问请联系教务处</div>
    `);
}
