"use client"

import * as React from "react"
import { ResponsiveContainer, BarChart, Bar, CartesianGrid, XAxis, YAxis, Tooltip, Legend } from "recharts"
import { ActivityRepPoint } from "./types"
import { NoDataFound } from "@/components/ui/no-data-found"

interface LeadsByRepChartProps {
    data: ActivityRepPoint[]
}

export const LeadsByRepChart = React.memo(function LeadsByRepChart({ data }: LeadsByRepChartProps) {
    if (!data.length) {
        return (
            <div className="bg-[rgba(255,255,255,0.45)] dark:bg-[rgba(244,246,255,0.4)] backdrop-blur-[15px] border border-white/60 rounded-[14px] p-6 shadow-[0px_2px_15px_0px_rgba(0,0,0,0.06)]">
                <h3 className="text-[13px] font-extrabold text-text font-['Lexend'] mb-0.5">Leads by Rep</h3>
                <p className="text-[11px] text-gray-700 font-medium mb-5 uppercase tracking-tight">Active · Won · Lost Leads</p>
                <NoDataFound message="No rep data" description="No leads in this period for the selected team." size="sm" />
            </div>
        )
    }

    return (
        <div className="bg-[rgba(255,255,255,0.45)] dark:bg-[rgba(244,246,255,0.4)] backdrop-blur-[15px] border border-white/60 rounded-[14px] p-6 shadow-[0px_2px_15px_0px_rgba(0,0,0,0.06)] hover:shadow-[0px_6px_25px_0px_rgba(108,99,255,0.1)] transition-all duration-300 hover:scale-[1.005]">
            <h3 className="text-[13px] font-extrabold text-text font-['Lexend'] mb-0.5">Leads by Rep</h3>
            <p className="text-[11px] text-gray-700 font-medium mb-5 uppercase tracking-tight">Active · Won · Lost Leads</p>

            <div className="h-[220px]">
                <ResponsiveContainer width="100%" height="100%">
                    <BarChart data={data} margin={{ top: 10, bottom: 20 }}>
                        <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#E4E6EF88" />
                        <XAxis
                            dataKey="label"
                            axisLine={false}
                            tickLine={false}
                            tick={{ fontSize: 10, fill: "#6B7280", fontWeight: "bold" }}
                            dy={5}
                        />
                        <YAxis
                            axisLine={false}
                            tickLine={false}
                            tick={{ fontSize: 10, fill: "#6B7280" }}
                        />
                        <Tooltip
                            cursor={{ fill: '#6C63FF08' }}
                            contentStyle={{
                                borderRadius: '12px',
                                border: '1px solid rgba(255, 255, 255, 0.2)',
                                backdropFilter: 'blur(10px)',
                                boxShadow: '0 8px 32px 0 rgba(31, 38, 135, 0.15)',
                                padding: '10px',
                                backgroundColor: 'rgba(15, 17, 23, 0.85)',
                                color: '#fff'
                            }}
                        />
                        <Legend
                            verticalAlign="bottom"
                            align="center"
                            iconType="circle"
                            wrapperStyle={{ fontSize: '10px', fontWeight: 'bold', paddingTop: '20px', textTransform: 'uppercase' }}
                        />
                        <Bar dataKey="active" name="Active" stackId="a" fill="#6C63FF88" radius={[0, 0, 0, 0]} barSize={34} />
                        <Bar dataKey="won" name="Won" stackId="a" fill="#22C55E88" radius={[0, 0, 0, 0]} barSize={34} />
                        <Bar dataKey="lost" name="Lost" stackId="a" fill="#F59E0B88" radius={[6, 6, 0, 0]} barSize={34} />
                    </BarChart>
                </ResponsiveContainer>
            </div>
        </div>
    )
})
