"use client";

import Button from "@/components/ui/buttons/Button";
import SectionWrapper from "@/components/ui/section/SectionWrapper";
import NotFound from "@/components/ui/NotFound";
import { Plus, Pencil, Trash2, ChevronRight, ChevronUp } from "lucide-react";
import { useEffect, useState } from "react";
import Loader from "@/components/ui/Loader/Loader";
import { AVATAR_COLORS } from "@/constants";
import { insuranceDummyData } from "@/data/client-master";
import { getExpiryDateStatus, getStatusStyles } from "@/utils/status.utils";
import { useExpand } from "@/hooks/useExpand";

export default function Insurance({ clientId }: { clientId: string }) {
  const [loading, setLoading] = useState(true);
  const { expanded, toggleExpand } = useExpand();

  useEffect(() => {
    const timer = setTimeout(() => setLoading(false), 500);
    return () => clearTimeout(timer);
  }, []);

  if (loading) {
    return (
      <div className="flex-1 overflow-auto px-6 py-5 ">
        <SectionWrapper
          title="Insurance Register"
          button={
            <Button
              name="Add Insurance"
              variant="outline"
              size="sm"
              icon={Plus}
              onClick={() => { }}
            />
          }
        >
          <Loader />
        </SectionWrapper>
      </div>
    );
  }

  return (
    <div className="flex-1 overflow-auto px-6 py-5">
      <SectionWrapper
        title="Insurance Register"
        button={
          <Button
            name="Add Insurance"
            variant="outline"
            size="sm"
            icon={Plus}
            onClick={() => { }}
          />
        }
      >
        {insuranceDummyData.length === 0 ? (
          <NotFound
            title="No insurance records added yet"
            message="Click 'Add Insurance' to get started."
          />
        ) : (
          <div className="space-y-2">
            {insuranceDummyData.map((record, idx) => {
              const status = getExpiryDateStatus(record.policyExpiryDate);
              const statusStyle = getStatusStyles(status);
              const open = expanded.has(record.id);

              return (
                <div
                  key={record.id}
                  className="border border-gray-200 rounded-xl bg-white overflow-hidden"
                >
                  {/* ── Main row ── */}
                  <div className="flex items-center gap-3 px-4 py-3">
                    {/* Company icon / initial */}
                    <div
                      className="w-9 h-9 rounded-full flex items-center justify-center text-white text-xs font-bold flex-shrink-0 select-none"
                      style={{
                        background: AVATAR_COLORS[idx % AVATAR_COLORS.length],
                      }}
                    >
                      {record.insuranceCompany.charAt(0)}
                    </div>

                    {/* Company name + asset + status badge */}
                    <div className="flex-1 min-w-0">
                      <div className="flex items-center gap-2 flex-wrap">
                        <span className="text-sm font-semibold text-gray-900">
                          {record.insuranceCompany}
                        </span>
                        {statusStyle && (
                          <span
                            className={`text-[9px] font-bold uppercase tracking-widest px-1.5 py-0.5 rounded ${statusStyle.bg} ${statusStyle.text}`}
                          >
                            {statusStyle.label}
                          </span>
                        )}
                      </div>
                      <div className="text-[11px] text-gray-500 mt-0.5">
                        {record.assetInsured}
                      </div>
                    </div>

                    {/* Right side — policy amount + actions */}
                    <div className="flex items-center gap-3 flex-shrink-0">
                      <span className="text-xs font-semibold text-gray-900 whitespace-nowrap">
                        {record.policyAmount}
                      </span>

                      {/* Edit / Delete — show on hover */}
                      <div className="flex items-center gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity">
                        <button className="p-1.5 rounded-lg text-muted-foreground hover:text-blue-600 hover:bg-blue-50 transition-colors">
                          <Pencil className="w-3.5 h-3.5" />
                        </button>
                        <button className="p-1.5 rounded-lg text-muted-foreground hover:text-red-500 hover:bg-red-50 transition-colors">
                          <Trash2 className="w-3.5 h-3.5" />
                        </button>
                      </div>

                      {/* Expand toggle */}
                      <button
                        onClick={() => toggleExpand(record.id)}
                        className="p-1 rounded-lg text-muted-foreground hover:text-gray-600 hover:bg-gray-100 transition-colors"
                      >
                        {open ? (
                          <ChevronUp className="w-4 h-4" />
                        ) : (
                          <ChevronRight className="w-4 h-4" />
                        )}
                      </button>
                    </div>
                  </div>

                  {/* ── Expanded details ── */}
                  {open && (
                    <div className="border-t border-gray-100 bg-gray-50 px-4 py-4">
                      {/* Action buttons visible in expanded */}
                      <div className="flex items-center gap-2 mb-4">
                        <button className="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium border border-gray-200 rounded-lg hover:bg-white transition-colors text-gray-600">
                          <Pencil className="w-3 h-3" /> Edit
                        </button>
                        <button className="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium border border-red-100 rounded-lg hover:bg-red-50 transition-colors text-red-500">
                          <Trash2 className="w-3 h-3" /> Remove
                        </button>
                      </div>

                      {/* Details grid */}
                      <div className="grid grid-cols-3 gap-x-8 gap-y-3">
                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Policy Amount
                          </p>
                          <p className="text-sm font-bold text-gray-900">
                            {record.policyAmount}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Last Premium Amount
                          </p>
                          <p className="text-sm font-bold text-gray-900">
                            {record.lastPremiumAmount}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Asset Insured
                          </p>
                          <p className="text-xs text-gray-800">
                            {record.assetInsured}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Policy Issue Date
                          </p>
                          <p className="text-xs text-gray-800">
                            {record.policyIssueDate}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Policy Expiry Date
                          </p>
                          <p className="text-xs text-gray-800">
                            {record.policyExpiryDate}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Status
                          </p>
                          {statusStyle ? (
                            <span
                              className={`inline-block text-[9px] font-bold uppercase tracking-widest px-1.5 py-0.5 rounded ${statusStyle.bg} ${statusStyle.text}`}
                            >
                              {statusStyle.label}
                            </span>
                          ) : (
                            <span className="text-xs text-gray-500">N/A</span>
                          )}
                        </div>

                        {/* New Fields */}
                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Insurance Agent Name
                          </p>
                          <p className="text-xs text-gray-800">
                            {record.insuranceAgentName || "N/A"}
                          </p>
                        </div>

                        <div>
                          <p className="text-[10px] font-semibold uppercase tracking-widest text-gray-400 mb-0.5">
                            Insurance Agent Mobile
                          </p>
                          <p className="text-xs text-gray-800">
                            {record.insuranceAgentMobile || "N/A"}
                          </p>
                        </div>
                      </div>
                    </div>
                  )}
                </div>
              );
            })}
          </div>
        )}
      </SectionWrapper>
    </div>
  );
}