import { persianDigits } from '@/lib/persianDate';
import { ScoreValue, SectionCard } from './shared';
import type { NumericSubjectRow } from '@/lib/reportCard';

type Props = {
  subjects: NumericSubjectRow[];
  /** 1 | 2 = فقط همان نوبت با ستون مستمر/پایانی، null = نمای سالانه با هر دو نوبت */
  term: number | null;
  title: string;
  icon: string;
};

/** جدول نمرات عددی — پایه‌ی مشترک برای کارنامه متوسطه اول و دوم. */
export function NumericSubjectsTable({ subjects, term, title, icon }: Props) {
  const annual = term == null;

  return (
    <SectionCard title={title} icon={icon} bodyPad={false}>
      {subjects.length === 0 ? (
        <p className="rc-empty">هنوز نمره‌ای برای این نوبت ثبت نشده است.</p>
      ) : (
        <div className="rc-table-scroll">
          <table className="rc-table">
            <thead>
              <tr>
                <th className="rc-table-subject-col">نام درس</th>
                {annual ? (
                  <>
                    <th>نوبت اول</th>
                    <th>نوبت دوم</th>
                    <th>نتیجه سالانه</th>
                  </>
                ) : (
                  <>
                    <th>مستمر</th>
                    <th>پایانی</th>
                    <th>نمره نوبت</th>
                  </>
                )}
                <th>ضریب</th>
                <th>وضعیت</th>
              </tr>
            </thead>
            <tbody>
              {subjects.map((row) => (
                <tr key={row.subjectId}>
                  <td className="rc-table-subject-col">{row.subject}</td>
                  {annual ? (
                    <>
                      <td><ScoreValue value={row.term1} /></td>
                      <td><ScoreValue value={row.term2} /></td>
                      <td><ScoreValue value={row.overall} strong /></td>
                    </>
                  ) : (
                    <>
                      <td><ScoreValue value={row.continuous} /></td>
                      <td><ScoreValue value={row.examFinal} /></td>
                      <td><ScoreValue value={row.overall} strong /></td>
                    </>
                  )}
                  <td className="rc-table-coefficient">{persianDigits(row.coefficient)}</td>
                  <td>
                    {row.overall == null ? (
                      <span className="rc-result-pending">—</span>
                    ) : row.passed ? (
                      <span className="rc-result-pass"><i className="bi bi-check-circle-fill" /> قبول</span>
                    ) : (
                      <span className="rc-result-fail"><i className="bi bi-x-circle-fill" /> مردود</span>
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      )}
    </SectionCard>
  );
}
