// NO import statements — loaded as global script
const StudentStatusFilter = ({
  selectedStatuses = [],        // [] means "All"
  selectedExamStatuses = [],    // [] means "All"
  onStatusChange,
  onExamStatusChange,
  showExamStatus = true,
  compact = false               // true = smaller pills, no labels
}) => {
  // Clean Standard Architecture:
  // Status = College Enrollment (Active, Passed out, Exited)
  // Result = Academic Result (Pass, ATKT [Allowed to Keep Term], Fail [Year Down / Excessive Backlogs])
  const ENROLLMENT_STATUSES = ['Active', 'Passed out', 'Exited'];
  const ACADEMIC_STATUSES = ['Pass', 'ATKT', 'Fail'];

  const STATUS_STYLES = {
    'Active':               'bg-emerald-100 text-emerald-700 border-emerald-300 hover:bg-emerald-200',
    'Passed out':           'bg-blue-100 text-blue-700 border-blue-300 hover:bg-blue-200',
    'Exited':               'bg-slate-100 text-slate-600 border-slate-300 hover:bg-slate-200',
    'Pass':                 'bg-green-100 text-green-700 border-green-300 hover:bg-green-200',
    'ATKT':                 'bg-amber-100 text-amber-700 border-amber-300 hover:bg-amber-200',
    'Fail':                 'bg-red-100 text-red-700 border-red-300 hover:bg-red-200',
  };

  const ACTIVE_STYLES = {
    'Active':               'bg-emerald-600 text-white border-emerald-700',
    'Passed out':           'bg-blue-600 text-white border-blue-700',
    'Exited':               'bg-slate-600 text-white border-slate-700',
    'Pass':                 'bg-green-600 text-white border-green-700',
    'ATKT':                 'bg-amber-500 text-white border-amber-600',
    'Fail':                 'bg-red-600 text-white border-red-700',
  };

  const toggleStatus = (val) => {
    if (!onStatusChange) return;
    const next = selectedStatuses.includes(val)
      ? selectedStatuses.filter(x => x !== val)
      : [...selectedStatuses, val];
    onStatusChange(next);
  };

  const toggleExamStatus = (val) => {
    if (!onExamStatusChange) return;
    const next = selectedExamStatuses.includes(val)
      ? selectedExamStatuses.filter(x => x !== val)
      : [...selectedExamStatuses, val];
    onExamStatusChange(next);
  };

  const pillBase = compact
    ? 'px-2.5 py-0.5 text-[10px] font-bold rounded-full border transition-all cursor-pointer select-none'
    : 'px-3 py-1.5 text-xs font-bold rounded-full border transition-all cursor-pointer select-none';

  const allPill = compact
    ? 'px-2.5 py-0.5 text-[10px] font-bold rounded-full border transition-all cursor-pointer select-none'
    : 'px-3 py-1.5 text-xs font-bold rounded-full border transition-all cursor-pointer select-none';

  return (
    <div className="flex flex-wrap gap-4 items-center">
      {/* College Status: Active vs Passed Out vs Exited */}
      <div className="flex flex-wrap gap-1.5 items-center">
        {!compact && <span className="text-[10px] font-black text-gray-400 uppercase tracking-widest mr-1">Status:</span>}
        <button
          onClick={() => onStatusChange && onStatusChange([])}
          className={`${allPill} ${selectedStatuses.length === 0
            ? 'bg-gray-800 text-white border-gray-900'
            : 'bg-white text-gray-500 border-gray-300 hover:bg-gray-100'}`}
        >
          All
        </button>
        {ENROLLMENT_STATUSES.map(s => (
          <button
            key={s}
            onClick={() => toggleStatus(s)}
            className={`${pillBase} ${selectedStatuses.includes(s) ? ACTIVE_STYLES[s] : STATUS_STYLES[s]}`}
          >
            {s}
          </button>
        ))}
      </div>

      {/* Result: Pass, ATKT (allowed to keep term), Fail (Year Down) */}
      {showExamStatus && (
        <div className="flex flex-wrap gap-1.5 items-center">
          {!compact && <span className="text-[10px] font-black text-gray-400 uppercase tracking-widest mr-1">Result:</span>}
          <button
            onClick={() => onExamStatusChange && onExamStatusChange([])}
            className={`${allPill} ${selectedExamStatuses.length === 0
              ? 'bg-gray-800 text-white border-gray-900'
              : 'bg-white text-gray-500 border-gray-300 hover:bg-gray-100'}`}
          >
            All
          </button>
          {ACADEMIC_STATUSES.map(s => (
            <button
              key={s}
              onClick={() => toggleExamStatus(s)}
              className={`${pillBase} ${selectedExamStatuses.includes(s) ? ACTIVE_STYLES[s] : STATUS_STYLES[s]}`}
            >
              {s}
            </button>
          ))}
        </div>
      )}
    </div>
  );
};

window.StudentStatusFilter = StudentStatusFilter;
