import React from 'react';
import { Icon } from './icon.js';

/**
 * Underline tabs for view switching (Board / Gantt / Dependencies).
 */
export interface TabItem {
  id: string;
  label: string;
  /** Lucide icon name */
  icon?: string;
  /** Mono count rendered after the label */
  count?: number;
}
export interface TabsProps {
  items: TabItem[];
  /** id of the active tab */
  active: string;
  onChange?: (id: string) => void;
  style?: React.CSSProperties;
}

const CSS = `
.ct-tabs {
  display: flex; gap: 2px;
  border-bottom: 1px solid var(--border-hairline);
}
.ct-tab {
  display: inline-flex; align-items: center; gap: 7px;
  font: 500 13.5px/1 var(--font-sans);
  color: var(--ink-2);
  background: none; border: none;
  padding: 10px 12px;
  margin-bottom: -1px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: color var(--duration-fast) var(--ease-out);
}
.ct-tab:hover { color: var(--ink-1); }
.ct-tab--active {
  color: var(--ink-1);
  font-weight: 600;
  border-bottom-color: var(--accent);
}
.ct-tab__count { font: 400 11.5px/1 var(--font-mono); color: var(--ink-3); }
`;
(function inject() {
  if (typeof document !== 'undefined' && !document.getElementById('ct-tabs-css')) {
    const s = document.createElement('style'); s.id = 'ct-tabs-css'; s.textContent = CSS;
    document.head.appendChild(s);
  }
})();

export function Tabs({ items, active, onChange, style, ...rest }: TabsProps) {
  return (
    <div className="ct-tabs" role="tablist" style={style} {...rest}>
      {items.map((item) => (
        <button
          key={item.id}
          type="button"
          role="tab"
          aria-selected={item.id === active}
          className={`ct-tab${item.id === active ? ' ct-tab--active' : ''}`}
          onClick={() => onChange && onChange(item.id)}
        >
          {item.icon ? <Icon name={item.icon} size={15} /> : null}
          {item.label}
          {item.count != null ? <span className="ct-tab__count">{item.count}</span> : null}
        </button>
      ))}
    </div>
  );
}
