import { Division, DivisionResult } from '@/lib/db/division';
import { toBn } from '@/lib/utils';

interface DivisionComparisonTableProps {
  divisions: Division[];
  divisionResults: DivisionResult[];
}

export const DivisionComparisonTable = ({
  divisions,
  divisionResults,
}: DivisionComparisonTableProps) => {
  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">table_chart</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-3 md:px-4 py-2 sm:py-3 text-left text-xs font-bold text-gray-600 uppercase">
                বিভাগ
              </th>
              <th className="px-2 sm:px-3 md:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-600 uppercase">
                আসন
              </th>
              <th className="px-2 sm:px-3 md:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-600 uppercase">
                আওয়ামী
              </th>
              <th className="px-2 sm:px-3 md:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-600 uppercase">
                জাপা
              </th>
              <th className="px-2 sm:px-3 md:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-600 uppercase">
                স্বতন্ত্র
              </th>
              <th className="px-2 sm:px-3 md:px-4 py-2 sm:py-3 text-center text-xs font-bold text-gray-600 uppercase hidden sm:table-cell">
                প্রভাব বিস্তারকারী
              </th>
            </tr>
          </thead>
          <tbody className="divide-y divide-gray-100">
            {divisions.map((div, index) => {
              const result = divisionResults[index];
              const maxSeats = Math.max(result.AL, result.JP, result.IND);
              const winner = maxSeats === result.AL ? 'AL' : maxSeats === result.JP ? 'JP' : 'IND';
              const winnerName =
                maxSeats === result.AL
                  ? 'আওয়ামী লীগ'
                  : maxSeats === result.JP
                    ? 'জাতীয় পার্টি'
                    : 'স্বতন্ত্র';
              const winnerColor =
                maxSeats === result.AL
                  ? 'text-green-700 bg-green-50'
                  : maxSeats === result.JP
                    ? 'text-red-700 bg-red-50'
                    : 'text-gray-700 bg-gray-50';

              return (
                <tr key={div.id} className="hover:bg-gray-50 transition">
                  <td className="px-4 py-3">
                    <div className="flex items-center gap-2">
                      <div
                        className="w-3 h-3 rounded-full"
                        style={{ backgroundColor: div.color }}
                      ></div>
                      <span className="font-bold text-gray-800">{div.nameBn}</span>
                    </div>
                  </td>
                  <td className="px-4 py-3 text-center font-bold text-gray-800">
                    {toBn(div.seats)}
                  </td>
                  <td className="px-4 py-3 text-center text-green-700 font-bold">
                    {toBn(result.AL)}
                  </td>
                  <td className="px-4 py-3 text-center text-red-700 font-bold">
                    {toBn(result.JP)}
                  </td>
                  <td className="px-4 py-3 text-center text-gray-700 font-bold">
                    {toBn(result.IND)}
                  </td>
                  <td className="px-4 py-3 text-center hidden sm:table-cell">
                    <span
                      className={`px-2 py-1 rounded-full text-xs font-bold border border-opacity-20 ${winnerColor}`}
                    >
                      {winnerName}
                    </span>
                  </td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>
    </div>
  );
};
