// LocationCard.tsx

import { Location } from "@/types"
import { ChevronUp, ChevronRight, MapPin, Pencil, Trash2 } from "lucide-react"
import { locationTypeBadgeColor } from "@/utils/badge.utils"
import Badge from "@/components/ui/badge/badge"
import { LocationExpandedDetails } from "./LocationExpandedDetails"


interface LocationCardProps {
  location: Location
  idx: number
  open: boolean
  toggleExpand: (id: string) => void
  setEditTarget: (id: string) => void
  handleDelete: (id: string) => void
  handleDeleteDoc: (id: string) => void
}

export function LocationCard({
  location,
  idx,
  open,
  toggleExpand,
  setEditTarget,
  handleDelete,
  handleDeleteDoc,
}: LocationCardProps) {
  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">
        {/* Location Icon */}
        <div className="w-9 h-9 rounded-lg flex items-center justify-center flex-shrink-0 bg-gray-100">
          <MapPin className="w-5 h-5 text-gray-600" />
        </div>

        {/* Title + Metadata */}
        <div className="flex-1 min-w-0">
          <h3 className="text-sm font-semibold text-gray-900">
            {location.name}
          </h3>
          <div className="flex items-center gap-2 mt-0.5 flex-wrap">
            <Badge
              variant={locationTypeBadgeColor(location.locationType)}
            >
              {location.locationType}
            </Badge>
            <span className="text-[11px] text-gray-500">
              Lease till{" "}
              <span className="font-medium text-gray-700">
                {location.leaseEndDate}
              </span>
            </span>
          </div>
        </div>

        {/* Right - Rent + Actions */}
        <div className="flex items-center gap-3 flex-shrink-0">
          {/* Monthly Rent */}
          <div className="text-right">
            <p className="font-bold text-gray-900 text-sm">
              ₹
              {new Intl.NumberFormat("en-IN").format(
                location.monthlyRent,
              )}
            </p>
            <p className="text-xs text-gray-500">Monthly Rent</p>
          </div>

          {/* Edit / Delete - show on hover */}
          <div className="flex items-center gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity">
            <button
              onClick={() => setEditTarget(location.id)}
              className="p-1.5 rounded-lg text-gray-300 hover:text-blue-600 hover:bg-blue-50 transition-colors"
              title="Edit"
            >
              <Pencil className="w-3.5 h-3.5" />
            </button>
            <button
              onClick={() => handleDelete(location.id)}
              className="p-1.5 rounded-lg text-gray-300 hover:text-red-500 hover:bg-red-50 transition-colors"
              title="Delete"
            >
              <Trash2 className="w-3.5 h-3.5" />
            </button>
          </div>

          {/* Expand Toggle */}
          <button
            onClick={() => toggleExpand(location.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 && (
        <LocationExpandedDetails
          location={location}
          onEdit={() => setEditTarget(location.id)}
          onDelete={() => handleDelete(location.id)}
          onDeleteDoc={() => handleDeleteDoc(location.id)}
        />
      )}
    </div>
  )
}
