'use client';

import React from 'react';
import { inrFull, STATUS_LABEL, type Lead } from '../../lib/leads';
import { LeadStatusBadge } from './LeadBits';

const dash = (v: string | null | undefined) => (v ? v : '—');

export function formatFollowUpBadge(dateStr: string | null | undefined) {
  if (!dateStr) return <span className="text-[var(--tcx-muted)]">—</span>;
  const d = new Date(dateStr);
  if (isNaN(d.getTime())) return <span className="text-[var(--tcx-muted)]">—</span>;
  const now = new Date();
  const isPast = d.getTime() < now.getTime() && d.toDateString() !== now.toDateString();
  const isToday = d.toDateString() === now.toDateString();
  const formatted = d.toLocaleDateString('en-IN', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
  if (isToday)
    return (
      <span className="inline-flex items-center gap-1 font-semibold text-amber-600 dark:text-amber-400">
        <span className="h-1.5 w-1.5 rounded-full bg-amber-500" />
        Today, {d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
      </span>
    );
  if (isPast)
    return (
      <span className="inline-flex items-center gap-1 font-semibold text-rose-600 dark:text-rose-400">
        <span className="h-1.5 w-1.5 rounded-full bg-rose-500" />
        Overdue ({formatted})
      </span>
    );
  return <span className="font-medium text-[var(--tcx-text)]">{formatted}</span>;
}

/** Lead details responsive grid for record page tab. */
export function LeadDetailsGrid({ lead }: { lead: Lead }) {
  const rows: Array<[string, React.ReactNode]> = [
    ['Company', lead.company || '—'],
    ['Designation', dash(lead.title)],
    [
      'Email',
      lead.email ? (
        <a key="e" href={`mailto:${lead.email}`} className="font-medium text-[var(--tcx-primary)] hover:underline">
          {lead.email}
        </a>
      ) : (
        '—'
      ),
    ],
    ['Phone', dash(lead.phone)],
    ['Status', <LeadStatusBadge key="st" status={lead.status} />],
    ['Source', dash(lead.source)],
    ['Service interest', dash(lead.serviceInterest)],
    ['Owner', lead.owner?.name ?? 'Unassigned'],
    ['Estimated value', inrFull(lead.estimatedValue)],
    ['Next follow-up', formatFollowUpBadge(lead.nextFollowUpAt)],
    ['City', dash(lead.city)],
  ];
  return (
    <dl className="m-0 grid grid-cols-1 gap-x-6 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
      {rows.map(([k, v]) => (
        <div key={k} className="min-w-0">
          <dt className="text-[12px] text-[var(--tcx-muted)]">{k}</dt>
          <dd className="m-0 mt-0.5 break-words text-[13.5px] font-medium text-[var(--tcx-text)]">{v}</dd>
        </div>
      ))}
    </dl>
  );
}

/** Record page "Details" card (prototype label/value rows). */
export function LeadDetailsList({ lead }: { lead: Lead }) {
  const rows: Array<[string, React.ReactNode]> = [
    [
      'Email',
      lead.email ? (
        <a key="e" href={`mailto:${lead.email}`}>
          {lead.email}
        </a>
      ) : (
        '—'
      ),
    ],
    ['Phone', dash(lead.phone)],
    ['Company', lead.company],
    ['Designation', dash(lead.title)],
    ['Status', STATUS_LABEL[lead.status]],
    ['Source', dash(lead.source)],
    ['Service interest', dash(lead.serviceInterest)],
    ['Next follow-up', formatFollowUpBadge(lead.nextFollowUpAt)],
    ['Owner', lead.owner?.name ?? 'Unassigned'],
    ['Estimated value', inrFull(lead.estimatedValue)],
    ['City', dash(lead.city)],
  ];
  return (
    <div className="tcx-kv">
      {rows.map(([k, v]) => (
        <div key={k} className="tcx-kv-row">
          <span>{k}</span>
          <b className="break-words">{v}</b>
        </div>
      ))}
    </div>
  );
}
