'use client'

import { useState } from 'react'
import { useTranslations } from 'next-intl'
import { TrendingUp, CheckCircle, Clock, XCircle } from 'lucide-react'
import { useCurrencyStore } from '@/store/currencyStore'

export interface Earning {
  id: string
  from: string
  amount: number
  commission: number
  date: string
  status: 'pending' | 'completed' | 'cancelled'
}

interface EarningsHistoryProps {
  earnings: Earning[]
}

export default function EarningsHistory({ earnings }: EarningsHistoryProps) {
  const formatAmount = useCurrencyStore((s) => s.formatAmount)
  const t = useTranslations('Account.earnings')
  const [currentPage, setCurrentPage] = useState(1)
  const itemsPerPage = 5

  const totalPages = Math.ceil(earnings.length / itemsPerPage)
  const paginatedEarnings = earnings.slice(
    (currentPage - 1) * itemsPerPage,
    currentPage * itemsPerPage
  )

  if (earnings.length === 0) {
    return (
      <div className="bg-white rounded-xl shadow-sm border border-gray-100 p-8 text-center">
        <TrendingUp size={48} className="mx-auto text-gray-custom mb-3" />
        <h3 className="font-semibold text-dark mb-1">{t('noEarnings')}</h3>
        <p className="text-sm text-gray-custom">{t('noEarningsDescription')}</p>
      </div>
    )
  }

  return (
    <div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
      <div className="overflow-x-auto">
        <table className="w-full">
          <thead className="bg-gray-50 border-b border-gray-100">
            <tr>
              <th className="text-left p-4 text-sm font-semibold text-dark">{t('from')}</th>
              <th className="text-left p-4 text-sm font-semibold text-dark">{t('date')}</th>
              <th className="text-left p-4 text-sm font-semibold text-dark">{t('commission')}</th>
              <th className="text-left p-4 text-sm font-semibold text-dark">{t('orderAmount')}</th>
              <th className="text-right p-4 text-sm font-semibold text-dark">{t('status')}</th>
            </tr>
          </thead>
          <tbody className="divide-y divide-gray-100">
            {paginatedEarnings.map((earning) => (
              <tr key={earning.id} className="hover:bg-gray-50 transition">
                <td className="p-4">
                  <p className="font-medium text-dark">{earning.from}</p>
                </td>
                <td className="p-4 text-sm text-gray-custom">{new Date(earning.date).toLocaleDateString()}</td>
                <td className="p-4">
                  <span className="font-semibold text-primary">{formatAmount(earning.commission)}</span>
                </td>
                <td className="p-4 text-sm text-dark">{formatAmount(earning.amount)}</td>
                <td className="p-4 text-right">
                  {earning.status === 'completed' ? (
                    <span className="inline-flex items-center gap-1 text-green-600 bg-green-50 px-2 py-1 rounded-full text-xs">
                      <CheckCircle size={12} /> {t('completed')}
                    </span>
                  ) : earning.status === 'cancelled' ? (
                    <span className="inline-flex items-center gap-1 text-red-500 bg-red-50 px-2 py-1 rounded-full text-xs">
                      <XCircle size={12} /> {t('cancelled')}
                    </span>
                  ) : (
                    <span className="inline-flex items-center gap-1 text-yellow-600 bg-yellow-50 px-2 py-1 rounded-full text-xs">
                      <Clock size={12} /> {t('pendingStatus')}
                    </span>
                  )}
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      {totalPages > 1 && (
        <div className="flex justify-center gap-2 p-4 border-t border-gray-100">
          <button
            onClick={() => setCurrentPage(prev => Math.max(prev - 1, 1))}
            disabled={currentPage === 1}
            className="px-3 py-1 rounded-lg border border-gray-200 text-sm disabled:opacity-50"
          >
            {t('previous')}
          </button>
          <span className="px-3 py-1 text-sm text-dark">{t('pageOf', { current: currentPage, total: totalPages })}</span>
          <button
            onClick={() => setCurrentPage(prev => Math.min(prev + 1, totalPages))}
            disabled={currentPage === totalPages}
            className="px-3 py-1 rounded-lg border border-gray-200 text-sm disabled:opacity-50"
          >
            {t('next')}
          </button>
        </div>
      )}
    </div>
  )
}
