import { getPartnersAdmin, deletePartner } from '../../actions';
import { revalidatePath } from 'next/cache';
import PartnerRow from './PartnerRow';
import AddPartnerModal from './AddPartnerModal';

export default async function PartnersAdminPage() {
    const partners = await getPartnersAdmin();

    async function handleDelete(formData: FormData) {
        'use server';
        const id = Number(formData.get('id'));
        await deletePartner(id);
        revalidatePath('/admin/partners');
    }

    return (
        <div className="space-y-8">
            <div className="flex justify-between items-center">
                <div>
                    <h1 className="text-3xl font-black uppercase tracking-widest text-white mb-2">Partnerler</h1>
                    <p className="text-white/40">Referans markaların listesi.</p>
                </div>
                <AddPartnerModal />
            </div>

            <div className="bg-zinc-950 border border-white/10 rounded-2xl overflow-hidden shadow-2xl">
                <table className="w-full text-left">
                    <thead className="bg-white/5 border-b border-white/10">
                        <tr>
                            <th className="px-6 py-4 text-[10px] font-bold uppercase tracking-widest text-white/40 w-24">Sıra</th>
                            <th className="px-6 py-4 text-[10px] font-bold uppercase tracking-widest text-white/40">Logo Önizleme</th>
                            <th className="px-6 py-4 text-[10px] font-bold uppercase tracking-widest text-white/40">Marka Adı</th>
                            <th className="px-6 py-4 text-[10px] font-bold uppercase tracking-widest text-white/40">Durum</th>
                            <th className="px-6 py-4 text-[10px] font-bold uppercase tracking-widest text-white/40 text-right">İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        {partners.map((partner: any) => (
                            <PartnerRow key={partner.id} partner={partner} onDelete={handleDelete} />
                        ))}
                        {(!partners || partners.length === 0) && (
                            <tr>
                                <td colSpan={5} className="px-6 py-8 text-center text-white/40">
                                    Henüz partner eklenmemiş.
                                </td>
                            </tr>
                        )}
                    </tbody>
                </table>
            </div>
        </div>
    );
}
