"use client"

import { PlusCircle, Pencil, Trash2 } from "lucide-react"

import type { KnowledgePoint } from "../types"
import { cn } from "@/shared/lib/utils"
import { ScrollArea } from "@/shared/components/ui/scroll-area"
import { Badge } from "@/shared/components/ui/badge"
import { Button } from "@/shared/components/ui/button"

interface KnowledgePointListProps {
  knowledgePoints: KnowledgePoint[]
  canEdit: boolean
  highlightedKpId: string | null
  onHighlight: (id: string) => void
  onEdit: (kp: KnowledgePoint) => void
  onDelete: (kpId: string, e: React.MouseEvent) => void
  onCreateQuestion: (kp: KnowledgePoint) => void
}

export function KnowledgePointList({
  knowledgePoints,
  canEdit,
  highlightedKpId,
  onHighlight,
  onEdit,
  onDelete,
  onCreateQuestion,
}: KnowledgePointListProps) {
  if (knowledgePoints.length === 0) {
    return (
      <div className="flex h-full items-center justify-center text-muted-foreground p-4 text-center text-sm">
        该章节暂无知识点。
      </div>
    )
  }

  return (
    <ScrollArea className="flex-1 h-full px-2">
      <div className="space-y-2 pb-4">
        {knowledgePoints.map((kp) => (
          <button
            key={kp.id}
            type="button"
            className={cn(
              "w-full text-left p-3 rounded-lg border bg-card hover:bg-accent/50 transition-colors cursor-pointer",
              highlightedKpId === kp.id && "border-primary bg-primary/5"
            )}
            onClick={() => onHighlight(kp.id)}
          >
            <div className="flex items-start justify-between gap-2">
              <h4 className="text-sm font-medium leading-none">{kp.name}</h4>
              <div className="flex items-center gap-1">
                <Badge variant="outline" className="text-[10px] h-5 px-1">Lv.{kp.level}</Badge>
                {canEdit && (
                  <div className="flex items-center gap-1 ml-1">
                    <Button
                      variant="ghost"
                      size="icon"
                      className="h-5 w-5 text-muted-foreground hover:text-foreground"
                      onClick={(e) => {
                        e.stopPropagation()
                        onCreateQuestion(kp)
                      }}
                      title="创建相关题目"
                      aria-label="创建相关题目"
                    >
                      <PlusCircle className="h-3 w-3" />
                    </Button>
                    <Button
                      variant="ghost"
                      size="icon"
                      className="h-5 w-5 text-muted-foreground hover:text-foreground"
                      onClick={(e) => {
                        e.stopPropagation()
                        onEdit(kp)
                      }}
                      title="编辑知识点"
                      aria-label="编辑知识点"
                    >
                      <Pencil className="h-3 w-3" />
                    </Button>
                    <Button
                      variant="ghost"
                      size="icon"
                      className="h-5 w-5 text-muted-foreground hover:text-destructive"
                      onClick={(e) => onDelete(kp.id, e)}
                      title="删除知识点"
                      aria-label="删除知识点"
                    >
                      <Trash2 className="h-3 w-3" />
                    </Button>
                  </div>
                )}
              </div>
            </div>
            {kp.description && (
              <p className="mt-2 text-xs text-muted-foreground line-clamp-2">
                {kp.description}
              </p>
            )}
          </button>
        ))}
      </div>
    </ScrollArea>
  )
}
