import React from 'react';
import { Modal, Button, Spin, Tag, Row, Col } from 'antd';
import type { DuplicateCheckResponse } from '../interface';
import ReadMoreText from './ReadMoreText';

interface DuplicateLeadModalProps {
  open: boolean;
  duplicateResponse: DuplicateCheckResponse | null;
  onClose: () => void;
  onGoToLead?: (leadId: number) => void;
  onRevive?: (leadId: number) => void;
  onIgnore?: () => void;
  reviving?: boolean;
}

const DuplicateLeadModal: React.FC<DuplicateLeadModalProps> = ({
  open,
  duplicateResponse,
  onClose,
  onGoToLead,
  onRevive,
  onIgnore,
  reviving = false,
}) => {
  if (!duplicateResponse) return null;

  const { duplicateType, canRevive, duplicateLead } = duplicateResponse;
  const isSelf = duplicateType === 'self';
  const isTeam = duplicateType === 'team';
  const isSelfDiffLaunch = duplicateType === 'self_different_launch';

  // ─── Title and Subheading per scenario ──────────────────────────────────
  let mainTitle = '';
  let subHeading = '';

  if (isSelf || isSelfDiffLaunch) {
    mainTitle = 'Self Duplicacy';
    if (isSelf && !canRevive) subHeading = 'Lead already exists under your account in this launch';
    if (isSelf && canRevive) subHeading = 'Lead exists but is marked dead under your account';
    if (isSelfDiffLaunch) subHeading = 'Lead exists under your account in a different launch';
  } else if (isTeam) {
    mainTitle = 'Team Duplicacy';
    if (!canRevive) subHeading = 'Lead is already assigned to another team member in this launch';
    if (canRevive) subHeading = 'Lead was previously assigned and marked dead in this launch';
  }

  // ─── Footer per scenario ─────────────────────────────────────────────────
  let footer: React.ReactNode[] = [];

  if (isSelf && !canRevive) {
    // Scenario 1A – Go to Lead
    footer = [
      <Button
        key="go"
        type="primary"
        onClick={() => onGoToLead && onGoToLead(duplicateLead.lead_id)}
      >
        Go to Lead
      </Button>,
    ];
  } else if ((isSelf && canRevive) || isSelfDiffLaunch || (isTeam && canRevive)) {
    // Scenario 1B, 3, 2B – Revive + Ignore
    footer = [
      <Button key="ignore" onClick={onIgnore}>
        Ignore
      </Button>,
      <Button
        key="revive"
        type="primary"
        loading={reviving}
        onClick={() => onRevive && onRevive(duplicateLead.lead_id)}
      >
        Revive
      </Button>,
    ];
  }

  return (
    <Modal
      title={
        <div>
          <span className="page-header-title" style={{ display: 'block' }}>
            {mainTitle}
          </span>
          {subHeading && (
            <span style={{ fontSize: '13px', fontWeight: 'normal', color: '#666', marginTop: '4px', display: 'block' }}>
              {subHeading}
            </span>
          )}
        </div>
      }
      open={open}
      onCancel={onClose}
      maskClosable={false}
      keyboard={false}
      footer={footer.length > 0 ? footer : null}
      width={520}
      className="custom-new-modal"
      destroyOnClose
    >
      <Spin spinning={reviving}>
        <div className="duplicate-lead-modal__body" style={{ maxHeight: '50vh', overflowY: 'auto', paddingRight: '8px' }}>

          {/* ── Shared fields ── */}
          <div className="duplicate-lead-modal__grid">
            <Row gutter={[16, 16]}>
              <Col span={24}>
                <div className="duplicate-lead-modal__row">
                  <span className="duplicate-lead-modal__label">Name || Mobile</span>
                  <span className="duplicate-lead-modal__value">
                    {duplicateLead.lead_name || '–'} ({duplicateLead.mobile_number || '–'})
                  </span>
                </div>
              </Col>

              {/* Row 2: Stage || Status */}
              <Col span={24}>
                <div className="duplicate-lead-modal__row">
                  <span className="duplicate-lead-modal__label">Stage || Status</span>
                  <span className="duplicate-lead-modal__value">
                    <Tag color={duplicateLead.stage === 'Dead' ? 'red' : 'blue'} style={{ marginRight: '8px' }}>
                      {duplicateLead.stage || '–'}
                    </Tag>
                    ({duplicateLead.status || 'Assigned'})
                  </span>
                </div>
              </Col>

              {/* Row 3: Last Remark */}
              {duplicateLead.last_remark && (
                <Col span={24}>
                  <div className="duplicate-lead-modal__row">
                    <span className="duplicate-lead-modal__label">Last Remark</span>
                    <span className="duplicate-lead-modal__value duplicate-lead-modal__remark">
                      <ReadMoreText
                        text={duplicateLead.last_remark}
                        maxLines={1}
                      />
                    </span>
                  </div>
                </Col>
              )}

              {/* Row 4: Project */}
              {duplicateLead.project && (
                <Col span={24}>
                  <div className="duplicate-lead-modal__row">
                    <span className="duplicate-lead-modal__label">Project</span>
                    <span className="duplicate-lead-modal__value">
                      {duplicateLead.project}
                    </span>
                  </div>
                </Col>
              )}

              {/* Row 5: Launch */}
              {duplicateLead.launch && (
                <Col span={24}>
                  <div className="duplicate-lead-modal__row">
                    <span className="duplicate-lead-modal__label">Launch</span>
                    <span className="duplicate-lead-modal__value">
                      {duplicateLead.launch}
                    </span>
                  </div>
                </Col>
              )}
          </Row>
        </div>
      </div>
      </Spin>
    </Modal>
  );
};

export default DuplicateLeadModal;
