import { Badge } from "@/components/ui/badge";
import { differenceInDays } from "date-fns";

interface WarrantyStatusProps {
  expiry_date?: string;
}

export function WarrantyStatus({ expiry_date }: WarrantyStatusProps) {
  if (!expiry_date) {
    return <Badge variant="secondary">No expiry</Badge>;
  }

  const today = new Date();
  today.setHours(0, 0, 0, 0);

  const expiry = new Date(expiry_date);
  expiry.setHours(0, 0, 0, 0);

  const daysRemaining = differenceInDays(expiry, today);

  if (daysRemaining < 0) {
    return (
      <Badge variant="destructive">
        Expired {Math.abs(daysRemaining)} {Math.abs(daysRemaining) === 1 ? "day" : "days"} ago
      </Badge>
    );
  }

  if (daysRemaining <= 30) {
    return (
      <Badge className="bg-yellow-100 text-yellow-800 border-yellow-300 dark:bg-yellow-900/30 dark:text-yellow-400 dark:border-yellow-700">
        Expiring soon ({daysRemaining} {daysRemaining === 1 ? "day" : "days"})
      </Badge>
    );
  }

  return (
    <Badge className="bg-green-100 text-green-800 border-green-300 dark:bg-green-900/30 dark:text-green-400 dark:border-green-700">
      Active ({daysRemaining} {daysRemaining === 1 ? "day" : "days"} left)
    </Badge>
  );
}
