// DirectorCard.tsx

import { AVATAR_COLORS } from "@/constants"
import { Director } from "@/types"
import { nameInitials } from "@/utils"
import { calcAge } from "@/utils/calculate.utils"
import { ChevronUp, ChevronRight } from "lucide-react"
import { ActionButtons, Badge, Subtitle } from "./utils"
import { ExpandedDetails } from "./ExpandedDetails"


interface DirectorCardProps {
  dir: Director
  idx: number
  open: boolean
  toggleExpand: (id: number) => void
  setEditTarget: (id: string) => void
  handleDelete: (id: string) => void
}

export function DirectorCard({
  dir,
  idx,
  open,
  toggleExpand,
  setEditTarget,
  handleDelete,
}: DirectorCardProps) {
  const age = calcAge(dir.dob)
  const isSBO = dir.shareholdingPercent && Number(dir.shareholdingPercent) >= 25

  return (
    <div className="border border-gray-200 rounded-xl bg-white overflow-hidden group">
      <div className="flex items-center gap-3 px-4 py-3">
        <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] }}
        >
          {nameInitials(dir.name)}
        </div>

        <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">{dir.name}</span>
            {dir.isGuarantor && <Badge label="Guarantor" color="amber" />}
            {isSBO && <Badge label="SBO" color="blue" />}
          </div>
          <Subtitle dir={dir} age={age} />
        </div>

        <div className="flex items-center gap-3 flex-shrink-0">
          {dir.shareholdingPercent && (
            <span className="text-xs text-gray-500 font-medium">
              {Number(dir.shareholdingPercent).toFixed(1)}% shareholding
            </span>
          )}

          <ActionButtons
            onEdit={() => setEditTarget(dir.id)}
            onDelete={() => handleDelete(dir.id)}
          />

          <button
            onClick={() => toggleExpand(Number(dir.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>

      {open && <ExpandedDetails dir={dir} onEdit={() => setEditTarget(dir.id)} onDelete={() => handleDelete(dir.id)} />}
    </div>
  )
}
