import { mockDb } from '@/lib/mockDb'
import { approveSubmissionAction, rejectSubmissionAction } from '@/app/actions'
import { Check, X } from 'lucide-react'

export default async function AdminSubmissionsPage() {
  const submissions = await mockDb.getSubmissions()

  return (
    <div className="space-y-6">
      <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
        <div>
          <h2 className="text-3xl font-heading font-extrabold text-pine lowercase">işletme başvuruları</h2>
          <p className="text-ink/65 text-xs font-medium mt-1">
            Kullanıcılar tarafından `/isletme-ekle` formu üzerinden gönderilen ve onay bekleyen başvurular.
          </p>
        </div>
      </div>

      <div className="bg-paper border border-pine/8 rounded-2xl shadow-sm overflow-hidden">
        <div className="overflow-x-auto">
          <table className="min-w-full divide-y divide-pine/8 text-sm">
            <thead className="bg-stone-deep/40 text-shutter font-mono text-[10px] uppercase tracking-wider">
              <tr>
                <th className="px-6 py-4 text-left">İşletme</th>
                <th className="px-6 py-4 text-left">Konum & Kategori</th>
                <th className="px-6 py-4 text-left">Gönderen</th>
                <th className="px-6 py-4 text-left">Görsel</th>
                <th className="px-6 py-4 text-left">Durum</th>
                <th className="px-6 py-4 text-right">İşlemler</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-dashed divide-pine/8 text-ink/80">
              {submissions.length === 0 ? (
                <tr>
                  <td colSpan={6} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Kayıtlı işletme başvurusu bulunmamaktadır.
                  </td>
                </tr>
              ) : (
                submissions.map((sub) => {
                  return (
                    <tr key={sub.id} className="hover:bg-stone/20 transition duration-150">
                      <td className="px-6 py-4">
                        <div className="font-heading font-bold text-pine lowercase text-sm">{sub.businessName}</div>
                        <div className="text-xs text-ink/70 mt-1 max-w-xs line-clamp-2 leading-relaxed">{sub.description}</div>
                        <div className="text-[11px] text-shutter mt-1.5">{sub.address}</div>
                      </td>
                      <td className="px-6 py-4 font-mono text-xs">
                        <div className="text-pine font-semibold">Kategori: {sub.categoryId}</div>
                        <div className="text-shutter mt-0.5">Bölge: {sub.neighborhoodId}</div>
                      </td>
                      <td className="px-6 py-4">
                        <div className="text-xs font-semibold text-pine">{sub.contactName}</div>
                        <a href={`mailto:${sub.contactEmail}`} className="text-xs text-turquoise hover:underline">
                          {sub.contactEmail}
                        </a>
                        {sub.phone && <div className="text-[11px] text-ink/65 font-mono mt-1">{sub.phone}</div>}
                      </td>
                      <td className="px-6 py-4">
                        {sub.imageUrl ? (
                          <div className="w-16 h-12 relative rounded-lg border border-pine/8 overflow-hidden">
                            <img src={sub.imageUrl} alt={sub.businessName} className="w-full h-full object-cover" />
                          </div>
                        ) : (
                          <span className="text-[10px] font-mono text-shutter">Görsel Yok</span>
                        )}
                      </td>
                      <td className="px-6 py-4">
                        <span className={`inline-flex items-center rounded-full px-2.5 py-0.5 text-[10px] font-mono font-bold border uppercase tracking-wider ${
                          sub.status === 'PENDING' ? 'bg-paper text-gold border-gold/20' :
                          sub.status === 'APPROVED' ? 'bg-paper text-turquoise border-turquoise/20' :
                          'bg-paper text-bougainvillea border-bougainvillea/20'
                        }`}>
                          {sub.status === 'PENDING' ? 'Bekliyor' : sub.status === 'APPROVED' ? 'Onaylandı' : 'Reddedildi'}
                        </span>
                      </td>
                      <td className="px-6 py-4 text-right">
                        {sub.status === 'PENDING' && (
                          <div className="flex justify-end gap-2">
                            <form action={async () => {
                              'use server'
                              await approveSubmissionAction(sub.id)
                            }}>
                              <button
                                type="submit"
                                className="p-1.5 bg-paper text-turquoise hover:bg-turquoise/5 rounded-lg border border-turquoise/20 transition shadow-sm"
                                title="Onayla ve Mekanlara Ekle"
                              >
                                <Check className="w-4 h-4" />
                              </button>
                            </form>

                            <form action={async () => {
                              'use server'
                              await rejectSubmissionAction(sub.id)
                            }}>
                              <button
                                type="submit"
                                className="p-1.5 bg-paper text-bougainvillea hover:bg-bougainvillea/5 rounded-lg border border-bougainvillea/20 transition shadow-sm"
                                title="Reddet"
                              >
                                <X className="w-4 h-4" />
                              </button>
                            </form>
                          </div>
                        )}
                      </td>
                    </tr>
                  )
                })
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  )
}
