import React from 'react';
import { Download } from 'lucide-react';
import { cn } from '@/utils/utils';
import type { Document } from '@/types/Document';
import { Button } from './Button';
import { useDocumentDownload } from '@/hooks/useDocumentDownload';

interface DocumentObjectProps {
    doc: Document;
}

export const DocumentObject: React.FC<DocumentObjectProps> = ({ doc }) => {
    // 2. On initialise le Hook pour ce document spécifique
    const { downloadFile, isDownloading } = useDocumentDownload();

    const getCategoryBadge = (category: string) => {
        switch (category) {
            case 'Audit': return 'bg-purple-50 text-purple-700 border-purple-200';
            case 'Facture': return 'bg-slate-100 text-slate-700 border-slate-200';
            case 'Rapport de conformité': return 'bg-emerald-50 text-emerald-700 border-emerald-200';
            default: return 'bg-blue-50 text-blue-700 border-blue-200';
        }
    };

    return (
        <tr key={doc.id} className={cn('hover:bg-amber-50 hover:dark:bg-slate-600 cursor-default transition-all duration-300')}>
            <td className="px-6 py-4 whitespace-nowrap">
                <div className="flex items-center">
                    <svg className="shrink-0 h-6 w-6 text-slate-400 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                    </svg>
                    <div>
                        <div className="text-sm font-medium text-slate-900 dark:text-slate-100">{doc.title}</div>
                        <div className="text-xs text-slate-500 dark:text-slate-400">{doc.file} • PDF</div>
                    </div>
                </div>
            </td>
            <td className="px-6 py-4 whitespace-nowrap">
                <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium border ${getCategoryBadge(doc.category)}`}>
                    {doc.category}
                </span>
            </td>
            <td className="px-6 py-4 whitespace-nowrap text-sm text-slate-500 dark:text-slate-400">
                {/* Petit conseil d'optimisation : utiliser une fonction date pour éviter les crashs si doc.created est mal formaté */}
                {doc.created ? `${doc.created.split(" ")[0]} ${doc.created.split(" ")[1]?.substring(0,5)}` : 'N/A'}
            </td>
            <td className="px-6 py-4 items-center">
                {/* 3. On relie le bouton à l'état et à la fonction du Hook */}
                <Button 
                    className="text-blue-900 dark:text-blue-400 hover:text-blue-700 px-3 py-1.5 flex ml-auto" 
                    size='sm'
                    leftIcon={<Download className="w-4 h-4" />}
                    onClick={() => downloadFile(doc)}
                    isLoading={isDownloading} // Si votre composant Button supporte cette prop !
                    disabled={isDownloading}
                > 
                    {isDownloading ? 'Téléchargement...' : 'Télécharger'}
                </Button>
            </td>
        </tr>
    );
};