import { memo } from 'react';
import { LocalTask } from '../../api';
import { relativeTime, statusTone, formatStatusLabel } from '../../lib/utils';
import { ownerDisplayName } from '../../lib/owner';

interface LocalTaskListItemProps {
  task: LocalTask;
  active: boolean;
  onClick: () => void;
}

export const LocalTaskListItem = memo(function LocalTaskListItem({ task, active, onClick }: LocalTaskListItemProps) {
  const status = task.latestJob?.status ?? 'queued';
  const tone = statusTone(status);

  return (
    <button
      onClick={onClick}
      className={`w-full text-left px-3 py-2.5 rounded-md border transition-colors ${
        active
          ? 'border-accent/60 bg-accent-soft'
          : 'border-hairline bg-canvas hover:bg-surface'
      }`}
    >
      <div className="flex items-center justify-between gap-2 min-w-0">
        <div className="flex items-center gap-1.5 min-w-0">
          <span className="text-[10px] font-mono text-slate-400 tabular-nums">#{task.id}</span>
          <span className="text-[13px] font-semibold text-slate-900 truncate">{task.title}</span>
        </div>
        <div className="flex-shrink-0 flex items-center gap-1.5">
          {task.subtaskCount != null && task.subtaskCount > 0 && (
            <span className="text-[10px] font-mono text-slate-400 tabular-nums">
              {task.subtaskCompleted ?? 0}/{task.subtaskCount}
            </span>
          )}
          <span
            className="px-1.5 py-0.5 rounded text-[10px] font-medium border"
            style={{ background: tone.bg, color: tone.fg, borderColor: 'transparent' }}
          >
            {formatStatusLabel(status)}
          </span>
        </div>
      </div>
      <div className="mt-1 text-2xs text-slate-500 truncate leading-snug">
        {task.body.length > 80 ? `${task.body.slice(0, 80)}…` : task.body}
      </div>
      <div className="mt-1.5 flex items-center gap-1.5 text-[10px]">
        <span className="font-mono text-slate-400 tabular-nums">{relativeTime(task.updatedAt)}</span>
        <span className="text-slate-300">·</span>
        <span className="text-slate-600">{ownerDisplayName(task.ownerId, task.ownerName)}</span>
        {task.visibility === 'private' && (
          <span className="px-1 rounded text-[10px] font-medium bg-amber-50 dark:bg-amber-500/15 text-amber-700 dark:text-amber-300 border border-amber-100 dark:border-amber-500/30" title="Private">private</span>
        )}
        {task.visibility === 'org' && (
          <span className="px-1 rounded text-[10px] font-medium bg-blue-50 dark:bg-blue-500/15 text-blue-700 dark:text-blue-300 border border-blue-100 dark:border-blue-500/30" title={`Shared with ${task.visibilityScopeOrgName ?? 'org'}`}>
            {task.visibilityScopeOrgName ?? 'org'}
          </span>
        )}
        {task.visibility === 'public' && (
          <span className="px-1 rounded text-[10px] font-medium bg-emerald-50 dark:bg-emerald-500/15 text-emerald-700 dark:text-emerald-300 border border-emerald-100 dark:border-emerald-500/30" title="Public">public</span>
        )}
      </div>
    </button>
  );
});
