import React, { useState, useMemo } from 'react'; import { useTranslation } from '@/hooks/useI18n.tsx'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/Card'; import { Button } from '@/components/ui'; import { Avatar } from '@/components/ui'; import { Badge } from '@/components/ui'; import { useOrganizations } from '@/hooks/useOrganizations.ts'; import { Organization } from '@/types.ts'; import { useVerifyOrganization, useRejectVerification, useBulkVerifyOrganizations, } from '@/hooks/api/useAdminAPI.ts'; import { useToast } from '@/hooks/useToast.ts'; import { CheckCircle, XCircle, Eye, Clock, AlertTriangle, ArrowLeft } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; const AdminVerificationQueuePage = () => { const { t } = useTranslation(); const navigate = useNavigate(); const { organizations } = useOrganizations(); const { success, error: showError } = useToast(); const [selectedOrg, setSelectedOrg] = useState(null); const [rejectionReason, setRejectionReason] = useState(''); const [rejectionNotes, setRejectionNotes] = useState(''); // API hooks const verifyOrganization = useVerifyOrganization(); const rejectVerification = useRejectVerification(); const bulkVerifyOrganizations = useBulkVerifyOrganizations(); // Filter organizations needing verification const pendingOrganizations = useMemo(() => { return organizations .filter((org) => !org.Verified && (org.PendingVerification || !org.Verified)) .sort((a, b) => { // Sort by creation date (newest first) return new Date(b.CreatedAt || '').getTime() - new Date(a.CreatedAt || '').getTime(); }); }, [organizations]); const handleVerify = async (org: Organization, notes?: string) => { try { await verifyOrganization.mutateAsync({ id: org.ID, notes }); success('Organization verified successfully'); setSelectedOrg(null); } catch { showError('Failed to verify organization'); } }; const handleReject = async (org: Organization) => { if (!rejectionReason) { showError('Please provide a rejection reason'); return; } try { await rejectVerification.mutateAsync({ id: org.ID, reason: rejectionReason, notes: rejectionNotes, }); success('Organization verification rejected'); setSelectedOrg(null); setRejectionReason(''); setRejectionNotes(''); } catch { showError('Failed to reject verification'); } }; const handleBulkVerify = async () => { const orgIds = pendingOrganizations.slice(0, 10).map((org) => org.ID); // Verify first 10 try { await bulkVerifyOrganizations.mutateAsync(orgIds); success(`Verified ${orgIds.length} organizations successfully`); } catch { showError('Failed to bulk verify organizations'); } }; const formatDate = (dateString: string | undefined) => { if (!dateString) return 'Unknown'; return new Date(dateString).toLocaleDateString(); }; const getVerificationCriteria = (org: Organization) => { return [ { label: 'Valid contact information', met: !!(org.Website || (org.Address && org.Address.includes('@'))), }, { label: 'Complete profile', met: !!(org.Name && org.Description && org.Sector), }, { label: 'Appropriate content', met: !!(org.Description && org.Description.length > 20), }, { label: 'Logo quality', met: !!org.Logo, }, ]; }; if (pendingOrganizations.length === 0) { return (

{t('admin.verification.queue.allCaughtUp')}

{t('admin.verification.queue.noPendingMessage')}

); } return (
{/* Header */}

{t('admin.verification.queue.title')}

{t('admin.verification.queue.subtitle', { count: pendingOrganizations.length })}

{pendingOrganizations.length > 0 && ( )}
{/* Queue List */}
{t('admin.verification.queue.pendingOrganizations')} {pendingOrganizations.map((org, index) => (
setSelectedOrg(org)} >
{org.Name?.charAt(0).toUpperCase()}

{org.Name}

{org.Sector}

{formatDate(org.CreatedAt)}

{index < 10 && ( {t('admin.verification.queue.priority')} )}
))}
{/* Organization Details */}
{selectedOrg ? (

{selectedOrg.Name}

{selectedOrg.Sector}

{/* Organization Info */}

{t('admin.verification.queue.basicInformation')}

{t('admin.verification.queue.sector')} {' '} {selectedOrg.Sector}
{t('admin.verification.queue.type')} {' '} {selectedOrg.Subtype || 'Not specified'}
{t('admin.verification.queue.website')} {' '} {selectedOrg.Website ? ( {selectedOrg.Website} ) : ( 'Not provided' )}
{t('admin.verification.queue.created')} {' '} {formatDate(selectedOrg.CreatedAt)}

{t('admin.verification.queue.description')}

{selectedOrg.Description || 'No description provided'}

{/* Verification Criteria */}

{t('admin.verification.queue.verificationChecklist')}

{getVerificationCriteria(selectedOrg).map((criteria, index) => (
{criteria.met ? ( ) : ( )} {criteria.label}
))}
{/* Resources */} {(selectedOrg.Needs?.length > 0 || selectedOrg.Offers?.length > 0) && (
{selectedOrg.Needs && selectedOrg.Needs.length > 0 && (

{t('admin.verification.queue.needs')}

{selectedOrg.Needs.map((need, index) => ( {need} ))}
)} {selectedOrg.Offers && selectedOrg.Offers.length > 0 && (

{t('admin.verification.queue.offers')}

{selectedOrg.Offers.map((offer, index) => ( {offer} ))}
)}
)} {/* Action Buttons */}
) : (

{t('admin.verification.queue.selectOrganization')}

{t('admin.verification.queue.selectOrganizationDesc')}

)}
); }; export default AdminVerificationQueuePage;