import type { Connection } from '@/features/integrations/api.client';
import type { RecKey } from './data';

/*
 * Icons exactly as the design's icon() helper prints them on the Connected Apps pages
 * (includes/icons.php). Some differ from CrmIcon (e.g. caret-down, sort), so they live here.
 */
const PATHS = {
  store:
    '<path d="M4 9.5 5.5 4h13L20 9.5"/><path d="M4 9.5a2.7 2.7 0 0 0 5.3 0 2.7 2.7 0 0 0 5.4 0 2.7 2.7 0 0 0 5.3 0"/><path d="M5 12v8h14v-8M10 20v-5h4v5"/>',
  sort: '<path d="m7 15 5 5 5-5M7 9l5-5 5 5M12 4v16"/>',
  sparkles: '<path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2 2M16 16l2 2M6 18l2-2M16 8l2-2"/>',
  alert: '<path d="M12 3 2 20h20z"/><path d="M12 10v4M12 17h.01"/>',
  info: '<circle cx="12" cy="12" r="9"/><path d="M12 11v5M12 8h.01"/>',
  search: '<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>',
  'caret-down': '<path d="m7 10 5 5 5-5" />',
  'chevron-down': '<path d="m6 9 6 6 6-6"/>',
  'chevron-left': '<path d="m15 18-6-6 6-6"/>',
  'chevron-right': '<path d="m9 18 6-6-6-6"/>',
  calendar:
    '<rect x="3.5" y="5" width="17" height="15.5" rx="2"/><path d="M3.5 10h17M8 3v4M16 3v4"/>',
  x: '<path d="M6 6l12 12M18 6 6 18"/>',
  refresh:
    '<path d="M1 4V10H7" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/><path d="M23 20V14H17" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/><path d="M20.49 9.00008C19.9828 7.56686 19.1209 6.28548 17.9845 5.27549C16.8482 4.26551 15.4745 3.55984 13.9917 3.22433C12.5089 2.88883 10.9652 2.93442 9.50481 3.35685C8.04437 3.77928 6.71475 4.56479 5.64 5.64008L1 10.0001M23 14.0001L18.36 18.3601C17.2853 19.4354 15.9556 20.2209 14.4952 20.6433C13.0348 21.0657 11.4911 21.1113 10.0083 20.7758C8.52547 20.4403 7.1518 19.7346 6.01547 18.7247C4.87913 17.7147 4.01717 16.4333 3.51 15.0001" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>',
  mail: '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="m3.5 6.5 8.5 6 8.5-6"/>',
  user: '<circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/>',
  building:
    '<rect x="5" y="3" width="14" height="18" rx="1.5"/><path d="M9 7h2M13 7h2M9 11h2M13 11h2M9 15h2M13 15h2M10 21v-3h4v3"/>',
  file: '<path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path d="M14 3v5h5M9 13h6M9 17h6"/>',
  trash:
    '<path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2M10 11v6M14 11v6"/>',
  chart: '<path d="M4 4v16h16"/><path d="M8 16v-4M12 16V8M16 16v-6"/>',
  sun: '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>',
  layout: '<rect x="3.5" y="4" width="17" height="16" rx="2"/><path d="M3.5 9h17M9 9v11"/>',
  'check-circle': '<circle cx="12" cy="12" r="9"/><path d="m8 12 3 3 5-6"/>',
  send: '<path d="M21 3 10 14"/><path d="m21 3-6.5 18-4.5-7-7-4.5z"/>',
  settings:
    '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-settings preview-icon"><path d="M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915"/><circle cx="12" cy="12" r="3"/></svg>',
  box: '<path d="M21 8 12 3 3 8v8l9 5 9-5z"/><path d="m3 8 9 5 9-5M12 13v8"/>',
  list: '<path d="M9 6h11M9 12h11M9 18h11"/><circle cx="4.5" cy="6" r=".6"/><circle cx="4.5" cy="12" r=".6"/><circle cx="4.5" cy="18" r=".6"/>',
  inbox:
    '<path d="M3 13h5l1.5 3h5L16 13h5"/><path d="M5.5 5h13L21 13v6a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-6z"/>',
  upgrade: '<circle cx="12" cy="12" r="9"/><path d="m8 11 4-4 4 4M12 7v10"/>',
  clock: '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>',
  key: '<circle cx="7.5" cy="15.5" r="4.5"/><path d="m10.7 12.3 9.8-9.8M16 7l3 3M18.5 4.5l2 2"/>',
  webhook:
    '<path d="M18 16.98h-5.99c-1.1 0-1.95.94-2.48 1.9A4 4 0 0 1 2 17c.01-.7.2-1.4.57-2"/><path d="m6 17 3.13-5.78c.53-.97.1-2.18-.5-3.1a4 4 0 1 1 6.89-4.06"/><path d="m12 6 3.13 5.73C15.66 12.7 16.9 13 18 13a4 4 0 0 1 0 8"/>',
} as const;

export type IconName = keyof typeof PATHS;

export function Icon({
  name,
  size = 16,
  className,
}: {
  name: IconName;
  size?: number;
  className?: string;
}) {
  return (
    <svg
      className={`icon iconSize-${size}${className ? ` ${className}` : ''}`}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="1.8"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
      focusable="false"
      dangerouslySetInnerHTML={{ __html: PATHS[name] }}
    />
  );
}

/** Logo colour and icon of each connection, by `appKey` (then `type`). */
const CONNECTION_LOGOS: Record<string, { bg: string; icon: IconName }> = {
  slack: { bg: '#4a154b', icon: 'inbox' },
  google_sheets: { bg: '#0f9d58', icon: 'layout' },
  gmail: { bg: '#ea4335', icon: 'mail' },
  google_calendar: { bg: '#1a73e8', icon: 'calendar' },
  private_app: { bg: '#64748b', icon: 'box' },
  service_key: { bg: '#0e7c86', icon: 'key' },
};

export function connectionLogo(c: Pick<Connection, 'appKey' | 'type'>) {
  return CONNECTION_LOGOS[c.appKey] ?? CONNECTION_LOGOS[c.type] ?? { bg: '#64748b', icon: 'box' };
}

/** Padlock used by the "Uninstall" menu item. */
export function LockIcon() {
  return (
    <svg
      className="icon iconSize-14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
      <path d="M7 11V7a5 5 0 0 1 10 0v4" />
    </svg>
  );
}

/** External-link arrow used by the contact / review dialogs and "View all workflows". */
export function ExternalIcon({ className = 'icon iconSize-14' }: { className?: string }) {
  return (
    <svg
      className={className}
      width="14"
      height="14"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" />
      <polyline points="15 3 21 3 21 9" />
      <line x1="10" y1="14" x2="21" y2="3" />
    </svg>
  );
}

/** `render_rec_logo()`. */
export function RecLogo({ appKey }: { appKey: RecKey }) {
  switch (appKey) {
    case 'google-sheets':
      return (
        <svg width="34" height="42" viewBox="0 0 34 42" fill="none">
          <path
            d="M21.5 0H4C1.8 0 0 1.8 0 4V38C0 40.2 1.8 42 4 42H30C32.2 42 34 40.2 34 38V12.5L21.5 0Z"
            fill="#0F9D58"
          />
          <path d="M21.5 0V12.5H34L21.5 0Z" fill="#87CEAB" />
          <rect x="7" y="18" width="20" height="16" rx="1.5" fill="white" />
          <line x1="7" y1="23.5" x2="27" y2="23.5" stroke="#0F9D58" strokeWidth="1.5" />
          <line x1="7" y1="28.5" x2="27" y2="28.5" stroke="#0F9D58" strokeWidth="1.5" />
          <line x1="15" y1="18" x2="15" y2="34" stroke="#0F9D58" strokeWidth="1.5" />
        </svg>
      );
    case 'airtable':
      return (
        <svg width="38" height="34" viewBox="0 0 200 170" fill="none">
          <path
            d="M89.7 13.5L13.8 54.3c-4.3 2.3-4.3 8.5 0 10.8l75.9 40.8c6.4 3.4 14.2 3.4 20.6 0l75.9-40.8c4.3-2.3 4.3-8.5 0-10.8L110.3 13.5c-6.4-3.4-14.2-3.4-20.6 0z"
            fill="#ED3158"
          />
          <path
            d="M102.5 120.3L184.2 76.5c3.7-2 8.3.7 8.3 4.9v69.8c0 5-2.7 9.7-7.1 12.1l-75.9 40.8c-4.4 2.4-9.8-.8-9.8-5.8v-72.3c0-2.6 1.3-5 2.8-5.7z"
            fill="#18BFFF"
          />
          <path
            d="M15.8 76.5l81.7 43.8c1.5.7 2.8 3.1 2.8 5.7v72.3c0 5-5.4 8.2-9.8 5.8L14.6 163.3c-4.4-2.4-7.1-7.1-7.1-12.1V81.4c0-4.2 4.6-6.9 8.3-4.9z"
            fill="#FCB400"
          />
        </svg>
      );
    case 'hilos':
      return (
        <svg
          width="34"
          height="38"
          viewBox="0 0 34 38"
          fill="none"
          stroke="#6366F1"
          strokeWidth="3.5"
          strokeLinecap="round"
          strokeLinejoin="round"
        >
          <rect x="4" y="3" width="9" height="32" rx="4.5" />
          <rect x="21" y="3" width="9" height="32" rx="4.5" />
          <path d="M13 14H21M13 24H21" />
        </svg>
      );
    case 'timelinesai':
      return (
        <svg width="38" height="34" viewBox="0 0 40 34" fill="none">
          <rect
            x="1"
            y="9"
            width="12"
            height="12"
            rx="3"
            stroke="#6366F1"
            strokeWidth="2.5"
            fill="#EDE9FE"
          />
          <line x1="4" y1="15" x2="10" y2="15" stroke="#6366F1" strokeWidth="2" />
          <line x1="13" y1="15" x2="22" y2="15" stroke="#6366F1" strokeWidth="2" />
          <line x1="22" y1="5" x2="22" y2="25" stroke="#6366F1" strokeWidth="2" />
          <rect
            x="25"
            y="1"
            width="12"
            height="10"
            rx="3"
            stroke="#6366F1"
            strokeWidth="2.5"
            fill="#EDE9FE"
          />
          <line x1="28" y1="6" x2="34" y2="6" stroke="#6366F1" strokeWidth="2" />
          <rect
            x="25"
            y="19"
            width="12"
            height="10"
            rx="3"
            stroke="#6366F1"
            strokeWidth="2.5"
            fill="#EDE9FE"
          />
          <line x1="28" y1="24" x2="34" y2="24" stroke="#6366F1" strokeWidth="2" />
          <line x1="22" y1="6" x2="25" y2="6" stroke="#6366F1" strokeWidth="2" />
          <line x1="22" y1="24" x2="25" y2="24" stroke="#6366F1" strokeWidth="2" />
        </svg>
      );
    case 'zoom':
      return (
        <svg width="36" height="36" viewBox="0 0 24 24" fill="#2D8CFF">
          <rect width="24" height="24" rx="6" fill="#2D8CFF" />
          <path
            d="M4 8.5C4 7.67 4.67 7 5.5 7H14.5C15.33 7 16 7.67 16 8.5V15.5C16 16.33 15.33 17 14.5 17H5.5C4.67 17 4 16.33 4 15.5V8.5Z"
            fill="white"
          />
          <path d="M17 10.2L20 7.8V16.2L17 13.8V10.2Z" fill="white" />
        </svg>
      );
    case 'pandadoc':
      return (
        <svg width="32" height="38" viewBox="0 0 34 42" fill="none">
          <rect width="34" height="42" rx="6" fill="#10B981" />
          <path
            d="M10 13H24M10 20H24M10 27H18"
            stroke="white"
            strokeWidth="2.5"
            strokeLinecap="round"
          />
        </svg>
      );
    case 'slack':
      return (
        <svg width="36" height="36" viewBox="0 0 24 24" fill="none">
          <rect width="24" height="24" rx="6" fill="#4A154B" />
          <path
            d="M6 10H18M6 14H18M10 6V18M14 6V18"
            stroke="white"
            strokeWidth="2"
            strokeLinecap="round"
          />
        </svg>
      );
    case 'zapier':
      return (
        <svg width="36" height="36" viewBox="0 0 24 24" fill="none">
          <rect width="24" height="24" rx="6" fill="#FF4F00" />
          <path
            d="M12 4V20M4 12H20M6.3 6.3L17.7 17.7M6.3 17.7L17.7 6.3"
            stroke="white"
            strokeWidth="2.5"
            strokeLinecap="round"
          />
        </svg>
      );
    default:
      return <Icon name="box" size={24} />;
  }
}

/** Close (×) used by the design's own dialogs (`ca-*-modal`). */
export function ModalCloseIcon({ strokeWidth = 1.7 }: { strokeWidth?: number }) {
  return (
    <svg
      viewBox="0 0 24 24"
      width="20"
      height="20"
      fill="none"
      stroke="currentColor"
      strokeWidth={strokeWidth}
      strokeLinecap="round"
    >
      <path d="M18 6 6 18M6 6l12 12" />
    </svg>
  );
}
