import { useTranslation } from 'react-i18next';
import { ChevronRight, Pencil, Trash2, Check, RefreshCw } from 'lucide-react';
import {
  ContextMenu,
  ContextMenuTrigger,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSub,
  ContextMenuSubTrigger,
  ContextMenuSubContent,
} from '@/components/ui/context-menu';
import { TableRow, TableCell } from '@/components/ui/table';
import { cn } from '@/lib/utils';
import { formatDueDate, isOverdue, type FormatPrefs } from '@/lib/date';
import { parseAssignees } from './task-utils';
import type { TaskItem } from './task-types';
import { PriorityBadge } from './PriorityBadge';
import { AssigneeStack } from './AssigneeStack';

const STATUSES = ['todo', 'in_progress', 'done'] as const;

export function TaskTableRow({
  task,
  hideProjectColumn,
  onClick,
  onEdit,
  onDelete,
  onStatusChange,
  prefs,
}: {
  task: TaskItem;
  hideProjectColumn?: boolean;
  onClick: () => void;
  onEdit: () => void;
  onDelete: () => void;
  onStatusChange: (status: string) => void;
  prefs: FormatPrefs;
}) {
  const { t } = useTranslation();
  const assignees = parseAssignees(task.assignee);
  const overdue = task.dueDate ? isOverdue(task.dueDate) && task.status !== 'done' : false;

  return (
    <ContextMenu>
      <ContextMenuTrigger asChild>
        <TableRow className="cursor-pointer" onClick={onClick}>
          <TableCell className="font-medium max-w-[280px] truncate">{task.title}</TableCell>
          {!hideProjectColumn && (
            <TableCell className="text-xs">
              {task.clientName && <span className="text-muted-foreground">{task.clientName}</span>}
              {task.clientName && task.projectName && (
                <ChevronRight className="inline h-3 w-3 mx-1 text-muted-foreground" />
              )}
              {task.projectName && <span>{task.projectName}</span>}
              {!task.projectName && <span className="text-muted-foreground">—</span>}
            </TableCell>
          )}
          <TableCell><PriorityBadge priority={task.priority} /></TableCell>
          <TableCell className={cn(overdue && 'text-red-600 dark:text-red-400')}>
            {task.dueDate ? formatDueDate(task.dueDate, prefs) : <span className="text-muted-foreground">—</span>}
          </TableCell>
          <TableCell><AssigneeStack assignees={assignees} /></TableCell>
        </TableRow>
      </ContextMenuTrigger>
      <ContextMenuContent>
        <ContextMenuItem onSelect={onEdit}>
          <Pencil className="h-4 w-4 mr-2" />
          {t('common.edit')}
        </ContextMenuItem>
        <ContextMenuSub>
          <ContextMenuSubTrigger>
            <RefreshCw className="h-4 w-4 mr-2" />
            {t('tasks.changeStatus')}
          </ContextMenuSubTrigger>
          <ContextMenuSubContent>
            {STATUSES.map((s) => (
              <ContextMenuItem key={s} onSelect={() => onStatusChange(s)}>
                {task.status === s ? <Check className="h-3 w-3 mr-2" /> : <span className="w-5" />}
                {t(s === 'todo' ? 'tasks.toDo' : s === 'in_progress' ? 'tasks.inProgress' : 'tasks.done')}
              </ContextMenuItem>
            ))}
          </ContextMenuSubContent>
        </ContextMenuSub>
        <ContextMenuItem onSelect={onDelete} className="text-destructive focus:text-destructive">
          <Trash2 className="h-4 w-4 mr-2" />
          {t('common.delete')}
        </ContextMenuItem>
      </ContextMenuContent>
    </ContextMenu>
  );
}
