import { mockDb } from '@/lib/mockDb'
import { deleteCollectionAction } from '@/app/actions'
import { Link } from '@/i18n/routing'
import { Edit, Trash, Plus, Layers } from 'lucide-react'

export default async function AdminCollectionsPage() {
  const collections = await mockDb.getCollections()

  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">kürasyon seçkileri</h2>
          <p className="text-ink/65 text-xs font-medium mt-1">
            Rehberdeki işletmelerin tematik olarak gruplandırıldığı koleksiyonlar.
          </p>
        </div>
        <Link
          href="/admin/collections/new"
          className="inline-flex items-center gap-1.5 bg-turquoise hover:bg-turquoise/90 text-paper text-xs font-bold py-3 px-5 rounded-xl shadow-sm transition active:scale-95 duration-150"
        >
          <Plus className="w-4 h-4" />
          Yeni Seçki Ekle
        </Link>
      </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">Görsel</th>
                <th className="px-6 py-4 text-left">Koleksiyon Başlığı (TR)</th>
                <th className="px-6 py-4 text-left">URL Slug</th>
                <th className="px-6 py-4 text-left">Mekan Sayısı</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">
              {collections.length === 0 ? (
                <tr>
                  <td colSpan={5} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Henüz seçki eklenmemiş.
                  </td>
                </tr>
              ) : (
                collections.map((col) => {
                  const listingCount = col.listings?.length || 0
                  return (
                    <tr key={col.id} className="hover:bg-stone/20 transition duration-150">
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="h-12 w-16 relative rounded-lg overflow-hidden bg-stone border border-pine/8">
                          {col.coverImage ? (
                            <img src={col.coverImage} alt={col.titleTr} className="object-cover w-full h-full" />
                          ) : (
                            <div className="w-full h-full flex items-center justify-center text-shutter bg-stone/50">
                              <Layers className="w-5 h-5" />
                            </div>
                          )}
                        </div>
                      </td>
                      <td className="px-6 py-4 font-medium">
                        <div className="font-heading font-bold text-pine lowercase text-sm">{col.titleTr}</div>
                        <div className="text-xs text-ink/65 mt-0.5 line-clamp-1 max-w-xs">{col.descriptionTr}</div>
                      </td>
                      <td className="px-6 py-4 font-mono text-xs text-turquoise">
                        /collection/{col.slug}
                      </td>
                      <td className="px-6 py-4 font-mono text-xs text-pine font-semibold">
                        {listingCount} Mekan
                      </td>
                      <td className="px-6 py-4 text-right whitespace-nowrap">
                        <div className="flex justify-end gap-2">
                          <Link
                            href={`/admin/collections/${col.id}`}
                            className="p-1.5 bg-paper text-shutter hover:text-turquoise hover:bg-turquoise/5 rounded-lg border border-pine/10 hover:border-turquoise/25 transition shadow-sm"
                            title="Düzenle"
                          >
                            <Edit className="w-4 h-4" />
                          </Link>

                          <form action={async () => {
                            'use server'
                            await deleteCollectionAction(col.id)
                          }}>
                            <button
                              type="submit"
                              className="p-1.5 bg-paper text-shutter hover:text-bougainvillea hover:bg-bougainvillea/5 rounded-lg border border-pine/10 hover:border-bougainvillea/25 transition shadow-sm"
                              title="Sil"
                            >
                              <Trash className="w-4 h-4" />
                            </button>
                          </form>
                        </div>
                      </td>
                    </tr>
                  )
                })
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  )
}
