"use client"

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

interface DealSizeDistChartProps {
    data: DealSizeDist[]
}

export const DealSizeDistChart = React.memo(function DealSizeDistChart({ data }: DealSizeDistChartProps) {
    const hasDeals = data.some((d) => d.deals > 0)
    if (!hasDeals) {
        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">Deal Size Distribution</h3>
                <p className="text-[11px] text-gray-700 font-medium mb-4 uppercase tracking-tight">Volume by Revenue Range</p>
                <NoDataFound message="No won deals" description="Closed-won deal sizes will appear here." 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">Deal Size Distribution</h3>
            <p className="text-[11px] text-gray-700 font-medium mb-8 uppercase tracking-tight">Volume by Revenue Range</p>

            <div className="h-[200px]">
                <ResponsiveContainer width="100%" height="100%">
                    <BarChart data={data}>
                        <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="#E4E6EF88" />
                        <XAxis
                            dataKey="range"
                            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'
                            }}
                            itemStyle={{ color: '#fff', fontSize: '11px', fontWeight: 'bold' }}
                        />
                        <Bar dataKey="deals" fill="#6C63FF" radius={[6, 6, 0, 0]} barSize={40} />
                    </BarChart>
                </ResponsiveContainer>
            </div>
        </div>
    )
})
