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

export default async function AdminListingsPage() {
  const listings = await mockDb.getListings()

  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">mekanlar</h2>
          <p className="text-ink/65 text-xs font-medium mt-1">
            Marmaris Local rehberinde kayıtlı olan restoran, apart otel ve yerel hizmetlerin listesi.
          </p>
        </div>
        <Link
          href="/admin/listings/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 Mekan 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">Mekan</th>
                <th className="px-6 py-4 text-left">Kategori & Konum</th>
                <th className="px-6 py-4 text-left">Fiyat & Puan</th>
                <th className="px-6 py-4 text-left">Mühür</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">
              {listings.length === 0 ? (
                <tr>
                  <td colSpan={6} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Henüz kayıtlı mekan bulunmamaktadır.
                  </td>
                </tr>
              ) : (
                listings.map((l) => {
                  const mainImage = l.images && l.images.length > 0 ? l.images[0].url : 'https://images.unsplash.com/photo-1555396273-367ea4eb4db5?w=800&auto=format&fit=crop&q=80'
                  return (
                    <tr key={l.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">
                          <img src={mainImage} alt={l.nameTr} className="object-cover w-full h-full" />
                        </div>
                      </td>
                      <td className="px-6 py-4">
                        <div className="font-heading font-bold text-pine lowercase text-sm">{l.nameTr}</div>
                        <div className="text-xs text-shutter font-mono mt-1">/{l.slug}</div>
                      </td>
                      <td className="px-6 py-4 text-xs font-semibold">
                        <div className="text-pine">Kategori: {l.category?.nameTr}</div>
                        <div className="text-shutter mt-0.5">Bölge: {l.neighborhood?.nameTr}</div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap font-mono text-xs">
                        <div className="text-pine font-semibold">Fiyat: {'₺'.repeat(l.priceRange)}</div>
                        {l.rating && <div className="text-gold font-bold mt-0.5">★ {l.rating.toFixed(1)}</div>}
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        {l.isLocalApproved ? (
                          <span className="inline-flex items-center gap-1 rounded-full bg-paper px-2.5 py-0.5 text-[10px] font-mono font-bold text-turquoise border border-turquoise/20 uppercase tracking-wider">
                            <CheckCircle className="w-3.5 h-3.5 fill-turquoise/5" />
                            Yerel Onaylı
                          </span>
                        ) : (
                          <span className="inline-flex items-center gap-1 rounded-full bg-paper px-2.5 py-0.5 text-[10px] font-mono font-medium text-shutter/60 border border-pine/8 uppercase tracking-wider">
                            Normal
                          </span>
                        )}
                      </td>
                      <td className="px-6 py-4 text-right whitespace-nowrap">
                        <div className="flex justify-end gap-2">
                          <Link
                            href={`/admin/listings/${l.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 deleteListingAction(l.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>
  )
}
