import prisma from '@/lib/prisma'
import { Pencil, Plus } from 'lucide-react'
import Link from 'next/link'
import DeleteButton from './DeleteButton'

export const metadata = {
  title: 'Kategoriler - Admin',
}

export default async function CategoriesPage() {
  const categories = await prisma.categories.findMany({
    orderBy: { order_num: 'asc' },
    include: {
      _count: {
        select: { products: true }
      }
    }
  })

  return (
    <div className="space-y-6">
      <div className="flex justify-between items-center">
        <h1 className="text-2xl font-bold text-gray-900">Kategoriler</h1>
        <Link href="/admin/categories/new" className="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg font-medium flex items-center gap-2 transition-colors">
          <Plus size={18} /> Yeni Kategori Ekle
        </Link>
      </div>

      <div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div className="overflow-x-auto">
          <table className="w-full text-left border-collapse">
            <thead>
              <tr className="bg-gray-50 border-b border-gray-100 text-sm font-semibold text-gray-600">
                <th className="py-4 px-6">Kategori Adı</th>
                <th className="py-4 px-6 text-center">Sıra</th>
                <th className="py-4 px-6 text-center">Ürün Sayısı</th>
                <th className="py-4 px-6 text-center">Durum</th>
                <th className="py-4 px-6 text-right">İşlemler</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-gray-100">
              {categories.map((category) => (
                <tr key={category.id} className="hover:bg-gray-50 transition-colors">
                  <td className="py-4 px-6 font-medium text-gray-900">{category.name_tr}</td>
                  <td className="py-4 px-6 text-center text-gray-600">{category.order_num}</td>
                  <td className="py-4 px-6 text-center">
                    <span className="bg-blue-50 text-blue-700 py-1 px-3 rounded-full text-xs font-medium">
                      {category._count.products} Ürün
                    </span>
                  </td>
                  <td className="py-4 px-6 text-center">
                    <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${
                      category.status === 1 ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800'
                    }`}>
                      {category.status === 1 ? 'Aktif' : 'Pasif'}
                    </span>
                  </td>
                  <td className="py-4 px-6 text-right">
                    <div className="flex justify-end gap-2">
                      <Link href={`/admin/categories/${category.id}/edit`} className="p-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="Düzenle">
                        <Pencil size={18} />
                      </Link>
                      <DeleteButton id={category.id} />
                    </div>
                  </td>
                </tr>
              ))}
              {categories.length === 0 && (
                <tr>
                  <td colSpan={5} className="py-8 text-center text-gray-500">
                    Henüz kategori bulunmuyor.
                  </td>
                </tr>
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  )
}
