import { prisma } from '@/lib/prisma';
import ToggleVerifyButton from '@/components/ToggleVerifyButton';
import FeaturedModal from '@/components/FeaturedModal';
import EntrepreneurActions from '@/components/EntrepreneurActions';
import { ShieldAlert, ShieldX } from 'lucide-react';

async function getBusinesses() {
  return await prisma.business.findMany({
    where: {
      isActive: true
    },
    include: {
      owner: true,
    },
    orderBy: {
      createdAt: 'desc',
    },
  });
}

export default async function EntrepreneursPage() {
  const businesses = await getBusinesses();

  return (
    <div>
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-white mb-2">Gestion des Entrepreneurs</h1>
        <p className="text-slate-400">Valider ou révoquer les comptes des entreprises.</p>
      </div>

      <div className="card overflow-hidden p-0">
        <table className="data-table">
          <thead>
            <tr>
              <th>Entreprise</th>
              <th>Catégorie</th>
              <th>Propriétaire</th>
              <th>Statut</th>
              <th className="text-center">Visibilité</th>
              <th className="text-center">Mise en avant</th>
              <th className="text-right">Action</th>
            </tr>
          </thead>
          <tbody>
            {businesses.length === 0 ? (
              <tr>
                <td colSpan={7} className="text-center py-10 text-slate-500">
                  Aucun entrepreneur trouvé.
                </td>
              </tr>
            ) : (
              businesses.map((business: any) => (
                <tr key={business.id} className={(business.isSuspended || business.owner?.isSuspended) ? 'opacity-60 grayscale-[0.5]' : ''}>
                  <td>
                    <div className="flex items-center gap-3">
                      <div className="w-10 h-10 rounded-lg bg-slate-800 flex items-center justify-center overflow-hidden">
                        {business.logoUrl ? (
                          <img src={business.logoUrl} alt={business.name} className="w-full h-full object-cover" />
                        ) : (
                          <span className="text-xs text-slate-500">LOGO</span>
                        )}
                      </div>
                      <div>
                        <div className="font-semibold text-white">{business.name}</div>
                        <div className="text-xs text-slate-500">{business.location}</div>
                      </div>
                    </div>
                  </td>
                  <td>
                    <span className="text-sm text-slate-300">{business.category}</span>
                  </td>
                  <td>
                    <div className="text-sm text-white">{business.owner.name}</div>
                    <div className="text-xs text-slate-500">{business.owner.email}</div>
                  </td>
                  <td>
                    <div className="flex flex-col gap-1">
                        {business.owner?.isSuspended ? (
                            <span className="badge badge-error flex items-center gap-1">
                                <ShieldX className="w-3 h-3" /> Compte Suspendu
                            </span>
                        ) : business.isSuspended ? (
                            <span className="badge bg-orange-500/10 text-orange-500 border border-orange-500/20 flex items-center gap-1">
                                <ShieldAlert className="w-3 h-3" /> Shop Masqué
                            </span>
                        ) : business.verified ? (
                            <span className="badge badge-verified">Vérifié</span>
                        ) : (
                            <span className="badge badge-pending">En attente</span>
                        )}
                    </div>
                  </td>
                  <td className="text-center font-medium text-slate-300">
                    {business.viewCount.toLocaleString()}
                  </td>
                  <td className="text-center">
                    <FeaturedModal business={business} />
                  </td>
                  <td className="text-right">
                    <div className="flex items-center justify-end gap-3">
                        <ToggleVerifyButton id={business.id} verified={business.verified} />
                        <div className="h-8 w-[1px] bg-slate-800 mx-1" />
                        <EntrepreneurActions 
                            businessId={business.id} 
                            businessName={business.name}
                            userId={business.owner.id}
                            userName={business.owner.name}
                            isBusinessSuspended={!!business.isSuspended}
                            isUserSuspended={!!business.owner?.isSuspended}
                        />
                    </div>
                  </td>
                </tr>
              ))
            )}
          </tbody>
        </table>
      </div>
    </div>
  );
}
