"use client";
import { motion } from "framer-motion";
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Cell, ReferenceLine } from "recharts";
import { ChartDataPoint } from "@/lib/types";
import { formatCurrency } from "@/lib/portfolio";

interface PortfolioChartProps {
  data: ChartDataPoint[];
}

const CustomTooltip = ({ active, payload, label }: { active?: boolean; payload?: { name: string; value: number }[]; label?: string }) => {
  if (active && payload && payload.length) {
    return (
      <div className="rounded-phi p-3 text-phi-xs font-data"
        style={{ background: "rgba(13, 32, 68, 0.97)", border: "1px solid rgba(29, 111, 235, 0.3)", backdropFilter: "blur(12px)", boxShadow: "0 8px 32px rgba(0,0,0,0.5)" }}>
        <p className="font-display font-semibold mb-2" style={{ color: "#EEF5FF" }}>{label}</p>
        {payload.map((entry, i) => (
          <p key={i} style={{ color: entry.name === "pnl" ? (entry.value >= 0 ? "#00C875" : "#F03E3E") : entry.name === "value" ? "#4A90F5" : "#7A9ABF" }}>
            {entry.name === "investment" ? "Invested" : entry.name === "value" ? "Current" : "P&L"}: {formatCurrency(entry.value)}
          </p>
        ))}
      </div>
    );
  }
  return null;
};

export default function PortfolioChart({ data }: PortfolioChartProps) {
  return (
    <motion.div
      initial={{ opacity: 0, y: 20 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.6, delay: 0.3, ease: [0.22, 1, 0.36, 1] }}
      className="w-full"
    >
      <h3 className="font-display font-semibold text-phi-base mb-4" style={{ color: "#C8D8F0" }}>
        Investment vs Current Value
      </h3>
      <ResponsiveContainer width="100%" height={220}>
        <BarChart data={data} barGap={4} barCategoryGap="30%">
          <CartesianGrid strokeDasharray="3 3" stroke="rgba(29, 111, 235, 0.08)" vertical={false} />
          <XAxis dataKey="name" tick={{ fill: "#7A9ABF", fontSize: 10, fontFamily: "var(--font-jetbrains)" }} axisLine={false} tickLine={false} />
          <YAxis tick={{ fill: "#7A9ABF", fontSize: 10, fontFamily: "var(--font-jetbrains)" }} axisLine={false} tickLine={false}
            tickFormatter={(v) => formatCurrency(v, true)} width={70} />
          <Tooltip content={<CustomTooltip />} />
          <ReferenceLine y={0} stroke="rgba(29, 111, 235, 0.2)" />
          <Bar dataKey="investment" name="investment" radius={[3, 3, 0, 0]}>
            {data.map((_, idx) => <Cell key={idx} fill="rgba(29, 111, 235, 0.45)" />)}
          </Bar>
          <Bar dataKey="value" name="value" radius={[3, 3, 0, 0]}>
            {data.map((entry, idx) => (
              <Cell key={idx} fill={entry.pnl >= 0 ? "rgba(0, 200, 117, 0.75)" : "rgba(240, 62, 62, 0.75)"} />
            ))}
          </Bar>
        </BarChart>
      </ResponsiveContainer>
      <div className="flex gap-6 mt-3 justify-center">
        {[
          { color: "rgba(29, 111, 235, 0.5)", label: "Invested" },
          { color: "rgba(0, 200, 117, 0.75)", label: "Profit" },
          { color: "rgba(240, 62, 62, 0.75)", label: "Loss" },
        ].map(({ color, label }) => (
          <div key={label} className="flex items-center gap-2">
            <div className="w-3 h-3 rounded-sm" style={{ background: color }} />
            <span className="text-phi-xs font-data" style={{ color: "#7A9ABF" }}>{label}</span>
          </div>
        ))}
      </div>
    </motion.div>
  );
}
