import { Election } from '@/lib/db/history';
import { toBn } from '@/lib/utils';

interface HistoryAllElectionsTableProps {
  elections: Election[];
  onYearSelect: (year: number) => void;
}

export const HistoryAllElectionsTable = ({
  elections,
  onYearSelect,
}: HistoryAllElectionsTableProps) => {
  return (
    <div className="bg-white rounded-xl sm:rounded-2xl shadow-sm border border-gray-200 overflow-hidden">
      <div className="bg-linear-to-r from-gray-700 to-gray-800 text-white px-3 sm:px-4 md:px-6 py-2.5 sm:py-3 md:py-4">
        <h3 className="text-base sm:text-lg font-bold flex items-center gap-2">
          <span className="material-symbols-outlined">history</span>
          সকল নির্বাচনের সারাংশ
        </h3>
      </div>
      <div className="overflow-x-auto">
        <table className="w-full text-xs sm:text-sm">
          <thead className="bg-gray-50">
            <tr>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-left text-xs font-bold text-gray-700">
                সাল
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-left text-xs font-bold text-gray-700 hidden sm:table-cell">
                তারিখ
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-center text-xs font-bold text-green-700">
                আওয়ামী লীগ
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-center text-xs font-bold text-red-700">
                বিএনপি
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-center text-xs font-bold text-purple-700">
                জাতীয় পার্টি
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-700">
                অন্যান্য
              </th>
              <th className="px-2 sm:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-700 hidden sm:table-cell">
                ভোটার উপস্থিতি
              </th>
            </tr>
          </thead>
          <tbody className="divide-y divide-gray-100">
            {elections.map((election) => {
              const al = election.parties.find((p) => p.shortName === 'AL');
              const bnp = election.parties.find((p) => p.shortName === 'BNP');
              const jp = election.parties.find((p) => p.shortName === 'JP');
              const others = election.parties
                .filter((p) => !['AL', 'BNP', 'JP'].includes(p.shortName))
                .reduce((sum, p) => sum + p.seats, 0);

              return (
                <tr
                  key={election.year}
                  onClick={() => onYearSelect(election.year)}
                  className="hover:bg-gray-50 transition cursor-pointer"
                >
                  <td className="px-4 py-3 font-bold text-gray-800">{toBn(election.year)}</td>
                  <td className="px-4 py-3 text-gray-600 hidden sm:table-cell">{election.date}</td>
                  <td className="px-4 py-3 text-center font-bold text-green-700">
                    {toBn(al?.seats || 0)}
                  </td>
                  <td className="px-4 py-3 text-center font-bold text-red-700">
                    {toBn(bnp?.seats || 0)}
                  </td>
                  <td className="px-4 py-3 text-center font-bold text-purple-700">
                    {toBn(jp?.seats || 0)}
                  </td>
                  <td className="px-4 py-3 text-center text-gray-600">{toBn(others)}</td>
                  <td className="px-4 py-3 text-center hidden sm:table-cell">
                    <span className="bg-blue-100 text-blue-800 px-2 py-1 rounded-lg text-xs font-bold">
                      {election.turnout}
                    </span>
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>
    </div>
  );
};
