import { mockDb } from '@/lib/mockDb'
import { Link } from '@/i18n/routing'
import { Globe, Calendar, CheckCircle, ExternalLink, Plus, Edit } from 'lucide-react'

export default async function AdminWidgetPartnersPage() {
  const partners = await mockDb.getWidgetPartners()

  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">widget ortakları (harici)</h2>
          <p className="text-ink/65 text-xs font-medium mt-1">
            Kendi eklediğiniz veya anlaştığınız harici siteler. Bu sitelere widget embed kodunu vererek gösterim yapabilirsiniz.
          </p>
        </div>
        <Link
          href="/admin/widget-partners/new"
          className="inline-flex items-center justify-center gap-2 bg-pine text-white px-5 py-2.5 rounded-xl font-bold text-sm hover:bg-turquoise transition-colors shadow-sm"
        >
          <Plus className="w-4 h-4" />
          Yeni Ekle
        </Link>
      </div>

      {/* Stats row */}
      <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
        <div className="bg-paper p-6 rounded-2xl border border-pine/8 shadow-sm">
          <span className="text-[10px] font-mono text-shutter uppercase tracking-wider block mb-1">Toplam Partner</span>
          <span className="text-3xl font-heading font-extrabold text-pine leading-none">{partners.length}</span>
        </div>
        <div className="bg-paper p-6 rounded-2xl border border-pine/8 shadow-sm">
          <span className="text-[10px] font-mono text-shutter uppercase tracking-wider block mb-1">Aktif Olanlar</span>
          <span className="text-3xl font-heading font-extrabold text-turquoise leading-none">{partners.filter(p => p.isActive).length}</span>
        </div>
      </div>

      {/* Partner List Table */}
      <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">Site Adı</th>
                <th className="px-6 py-4 text-left">Site URL</th>
                <th className="px-6 py-4 text-left">Kayıt Tarihi</th>
                <th className="px-6 py-4 text-left">Bölge (Opsiyonel)</th>
                <th className="px-6 py-4 text-left">Durum</th>
                <th className="px-6 py-4 text-right">İşlem</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-dashed divide-pine/8 text-ink/80">
              {partners.length === 0 ? (
                <tr>
                  <td colSpan={6} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Henüz eklenmiş bir widget partneri bulunmuyor.
                  </td>
                </tr>
              ) : (
                partners.map((partner) => {
                  return (
                    <tr key={partner.id} className="hover:bg-stone/20 transition duration-150">
                      <td className="px-6 py-4 font-medium">
                        <span className="font-heading font-bold text-pine lowercase text-sm">
                          {partner.name}
                        </span>
                      </td>
                      <td className="px-6 py-4 font-mono text-xs">
                        <a
                          href={partner.url}
                          target="_blank"
                          rel="noopener noreferrer"
                          className="text-turquoise hover:underline flex items-center gap-1 font-medium"
                        >
                          <Globe className="w-3.5 h-3.5 shrink-0" />
                          {partner.url.replace('https://', '')}
                          <ExternalLink className="w-3 h-3" />
                        </a>
                      </td>
                      <td className="px-6 py-4 font-mono text-xs">
                        <div className="flex items-center gap-1">
                          <Calendar className="w-3.5 h-3.5 text-shutter/60" />
                          {new Date(partner.createdAt).toLocaleDateString('tr-TR')}
                        </div>
                      </td>
                      <td className="px-6 py-4 font-medium text-xs text-shutter">
                        {partner.neighborhoodSlug || 'Tümü (Karışık)'}
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        {partner.isActive ? (
                          <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" />
                            Aktif
                          </span>
                        ) : (
                          <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-shutter border border-shutter/20 uppercase tracking-wider">
                            Pasif
                          </span>
                        )}
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap text-right">
                        <Link
                          href={`/admin/widget-partners/${partner.id}`}
                          className="inline-flex items-center justify-center p-2 text-shutter hover:text-turquoise hover:bg-turquoise/10 rounded-lg transition-colors"
                        >
                          <Edit className="w-4 h-4" />
                        </Link>
                      </td>
                    </tr>
                  )
                })
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  )
}
