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

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

    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 Reports',
            value: statistics?.total_reports || 0,
            icon: 'ti-file',
            color: 'primary'
        },
        {
            title: 'Pending Reports',
            value: statistics?.pending_reports || 0,
            icon: 'ti-time',
            color: 'warning'
        },
        {
            title: 'Investigating',
            value: statistics?.investigating_reports || 0,
            icon: 'ti-search',
            color: 'info'
        },
        {
            title: 'Resolved',
            value: statistics?.resolved_reports || 0,
            icon: 'ti-check',
            color: 'success'
        },
        {
            title: 'Dismissed',
            value: statistics?.dismissed_reports || 0,
            icon: 'ti-close',
            color: 'danger'
        }
    ];

    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">Report Statistics</h4>
                                    <p className="text-muted">Overview of all reports and their status</p>
                                </div>
                                <div>
                                    <Link href="/admin/reports" className="btn btn-secondary mr-2">
                                        <i className="ti-list mr-2"></i>
                                        All Reports
                                    </Link>
                                    <Link href="/admin/reports/pending" className="btn btn-warning">
                                        <i className="ti-time mr-2"></i>
                                        Pending Reports
                                    </Link>
                                </div>
                            </div>
                            
                            <div className="row mt-4">
                                {statsCards.map((card, index) => (
                                    <div key={index} className="col-lg-2 col-md-4 col-sm-6 mb-4">
                                        <Card
                                            title={card.title}
                                            value={card.value}
                                            icon={card.icon}
                                            color={card.color}
                                        />
                                    </div>
                                ))}
                            </div>

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

