import { useEffect, useRef } from "react";
import { usePage } from '@inertiajs/react';
import { toast, Toaster } from "react-hot-toast";
import Card from "@/Components/Admin/ui/Card";

export default function VerificationStatistics({ statistics }) {
    const { flash } = usePage().props;
    const processedMessages = useRef(new Set());

    // Handle flash messages
    useEffect(() => {
        if (flash?.success) {
            const message = typeof flash.success === 'object' 
                ? flash.success.message 
                : flash.success;
            if (message && !processedMessages.current.has(`success-${message}`)) {
                toast.success(message);
                processedMessages.current.add(`success-${message}`);
            }
        }
        if (flash?.error) {
            const message = typeof flash.error === 'object' 
                ? flash.error.message 
                : flash.error;
            if (message && !processedMessages.current.has(`error-${message}`)) {
                toast.error(message);
                processedMessages.current.add(`error-${message}`);
            }
        }
    }, [flash]);

    const statsCards = [
        {
            title: 'Total Verifications',
            value: statistics?.total_verifications || 0,
            icon: 'ti-check-box',
            color: 'primary'
        },
        {
            title: 'Verified Properties',
            value: statistics?.verified_properties || 0,
            icon: 'ti-check',
            color: 'success'
        },
        {
            title: 'Unverified Properties',
            value: statistics?.unverified_properties || 0,
            icon: 'ti-close',
            color: 'warning'
        },
        {
            title: 'This Month',
            value: statistics?.verifications_this_month || 0,
            icon: 'ti-calendar',
            color: 'info'
        }
    ];

    return (
        <>
            <Toaster />
            <div className="row">
                <div className="col-lg-12 mt-4">
                    <div className="card">
                        <div className="card-body">
                            <div className="d-flex justify-content-between align-items-center mb-4">
                                <div>
                                    <h4 className="header-title">Verification Statistics</h4>
                                    <p className="text-muted">Overview of property verification activities</p>
                                </div>
                                <a 
                                    href="/admin/properties/verifications" 
                                    className="btn btn-primary"
                                >
                                    <i className="ti-list mr-2"></i>
                                    View All Verifications
                                </a>
                            </div>
                            
                            <div className="row mt-4">
                                {statsCards.map((card, index) => (
                                    <div key={index} className="col-lg-3 col-md-6 mb-4">
                                        <Card
                                            title={card.title}
                                            value={card.value}
                                            icon={card.icon}
                                            color={card.color}
                                        />
                                    </div>
                                ))}
                            </div>

                            {statistics?.verifications_by_verifier && Object.keys(statistics.verifications_by_verifier).length > 0 && (
                                <div className="row mt-4">
                                    <div className="col-lg-12">
                                        <div className="card">
                                            <div className="card-body">
                                                <h5 className="card-title">Verifications by Verifier</h5>
                                                <div className="table-responsive">
                                                    <table className="table table-hover">
                                                        <thead>
                                                            <tr>
                                                                <th>Verifier ID</th>
                                                                <th>Number of Verifications</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            {Object.entries(statistics.verifications_by_verifier).map(([verifierId, count]) => (
                                                                <tr key={verifierId}>
                                                                    <td>{verifierId}</td>
                                                                    <td>
                                                                        <span className="badge badge-primary">{count}</span>
                                                                    </td>
                                                                </tr>
                                                            ))}
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            )}
                        </div>
                    </div>
                </div>
            </div>
        </>
    );
}

