import React from 'react';
import { Tag, Tooltip } from 'antd';
import { displayDate } from '@/utills/helper';
import type { LeadsRecordI } from '../interface';
import LeadTagBadges from './LeadTagBadges';

interface CustomerNameCellProps {
  name: string;
  record: LeadsRecordI;
  /** Super Hot is read from the lead's stored tag; the other two tags come from the API flags on the record. */
  isSuperHot: boolean;
  /** Display names from lead_tags, so a rename there shows up here. */
  tagLabels: { last72Hr?: string; cross?: string; superHot?: string };
}

/**
 * The Customer Name cell of a leads table: the name, then its tags (Last 72 Hr, Cross, Super Hot) and the
 * "Transferred" indicator. Used by Enquiries and Super Hot Leads, so both show the same thing whichever way
 * the page was reached (a column's render must not be set on the shared column objects by one page only).
 */
const CustomerNameCell: React.FC<CustomerNameCellProps> = ({ name, record, isSuperHot, tagLabels }) => (
  <div style={{ display: 'flex', alignItems: 'center', flexWrap: 'wrap', gap: 6 }}>
    <span>{name}</span>
    <LeadTagBadges
      isLast72Hr={record.is_last_72_hr}
      isCross={record.is_cross_tagged}
      isSuperHot={isSuperHot}
      labels={tagLabels}
    />
    {record.is_transferred === 1 && (
      <Tooltip
        title={
          <div>
            {record.transfer_type === 'self_transfer' ? (
              <>
                <div style={{ fontWeight: 600, marginBottom: 4 }}>Self Transfer</div>
                <div>From {record.transferred_from_launch_name || 'Unknown'} to {record.launch_name || 'Unknown'}</div>
              </>
            ) : (
              <>
                <div style={{ fontWeight: 600, marginBottom: 4 }}>Lead Transfer</div>
                <div>From {record.transferred_from_employee_name || 'Unknown'} to {record.transferred_to_employee_name || 'Unknown'}</div>
              </>
            )}
            {record.transferred_at && <div>Transfer Date: {displayDate(record.transferred_at)}</div>}
            {record.transfer_remark && <div>Remark: {record.transfer_remark}</div>}
          </div>
        }
      >
        <Tag color="orange" style={{ fontSize: 10, lineHeight: '16px', padding: '0 4px', margin: 0, cursor: 'pointer' }}>Transferred</Tag>
      </Tooltip>
    )}
  </div>
);

export default CustomerNameCell;
