import { Button } from '@renderer/components/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@renderer/components/ui/popover';
import { Tooltip, TooltipContent, TooltipTrigger } from '@renderer/components/ui/tooltip';
import { cn } from '@renderer/lib/utils';
import { ArrowDownUp, ArrowUpDown, Calendar, Clock, GripVertical, User } from 'lucide-react';

export type KanbanSortField = 'updatedAt' | 'createdAt' | 'owner' | 'manual';

export interface KanbanSortState {
  field: KanbanSortField;
}

const SORT_OPTIONS: {
  field: KanbanSortField;
  label: string;
  description: string;
  icon: React.ReactNode;
}[] = [
  {
    field: 'updatedAt',
    label: '最近更新',
    description: '按最近更新时间排序',
    icon: <Clock size={14} />,
  },
  {
    field: 'createdAt',
    label: '创建时间',
    description: '按最新创建排序',
    icon: <Calendar size={14} />,
  },
  {
    field: 'owner',
    label: '负责人',
    description: '按负责人名称排序',
    icon: <User size={14} />,
  },
  {
    field: 'manual',
    label: '手动排序',
    description: '按拖拽顺序显示',
    icon: <GripVertical size={14} />,
  },
];

interface KanbanSortPopoverProps {
  sort: KanbanSortState;
  onSortChange: (sort: KanbanSortState) => void;
}

export const KanbanSortPopover = ({
  sort,
  onSortChange,
}: KanbanSortPopoverProps): React.JSX.Element => {
  const isNonDefault = sort.field !== 'updatedAt';

  return (
    <Popover>
      <Tooltip>
        <TooltipTrigger asChild>
          <PopoverTrigger asChild>
            <Button
              variant="ghost"
              size="sm"
              className="relative h-7 px-2 text-[var(--color-text-muted)] hover:text-[var(--color-text)]"
              aria-label="任务排序"
            >
              <ArrowUpDown size={14} />
              {isNonDefault && (
                <span className="absolute -right-1 -top-1 flex size-4 items-center justify-center rounded-full bg-blue-500 text-[10px] font-medium text-white">
                  1
                </span>
              )}
            </Button>
          </PopoverTrigger>
        </TooltipTrigger>
        <TooltipContent side="bottom">任务排序</TooltipContent>
      </Tooltip>
      <PopoverContent align="end" className="w-56 p-0">
        <div className="p-3">
          <p className="mb-2 text-[11px] font-medium uppercase tracking-wider text-[var(--color-text-muted)]">
            排序方式
          </p>
          <div className="space-y-0.5">
            {SORT_OPTIONS.map((option) => {
              const isSelected = sort.field === option.field;
              return (
                <button
                  key={option.field}
                  type="button"
                  className={cn(
                    'flex w-full items-center gap-2.5 rounded-md px-2 py-1.5 text-left text-xs transition-colors',
                    isSelected
                      ? 'bg-blue-500/15 text-blue-300'
                      : 'text-[var(--color-text-secondary)] hover:bg-[var(--color-surface-raised)]'
                  )}
                  onClick={() => onSortChange({ field: option.field })}
                >
                  <span
                    className={cn(
                      'shrink-0',
                      isSelected ? 'text-blue-400' : 'text-[var(--color-text-muted)]'
                    )}
                  >
                    {option.icon}
                  </span>
                  <div className="min-w-0">
                    <div className="font-medium">{option.label}</div>
                    <div
                      className={cn(
                        'text-[10px]',
                        isSelected ? 'text-blue-300/70' : 'text-[var(--color-text-muted)]'
                      )}
                    >
                      {option.description}
                    </div>
                  </div>
                  {isSelected && (
                    <ArrowDownUp size={12} className="ml-auto shrink-0 text-blue-400" />
                  )}
                </button>
              );
            })}
          </div>
        </div>
        {isNonDefault && (
          <div className="flex justify-end border-t border-[var(--color-border)] p-2">
            <Button
              variant="ghost"
              size="sm"
              className="h-6 px-2 text-[11px] text-[var(--color-text-muted)] hover:text-[var(--color-text)]"
              onClick={() => onSortChange({ field: 'updatedAt' })}
            >
              重置
            </Button>
          </div>
        )}
      </PopoverContent>
    </Popover>
  );
};
