"use client"

import { Plus, Trash2 } from "lucide-react"
import { useTranslations } from "next-intl"
import { Button } from "@/shared/components/ui/button"
import { Input } from "@/shared/components/ui/input"
import { Label } from "@/shared/components/ui/label"
import { Checkbox } from "@/shared/components/ui/checkbox"
import type { ExamNode } from "./assembly/selected-question-list"
import type { EditableQuestionContent } from "./exam-form-types"

type QuestionOptionsEditorProps = {
  selectedQuestionId: string
  selectedContent: EditableQuestionContent
  questionType: string
  updatePreviewQuestionNode: (questionId: string, updater: (node: ExamNode) => ExamNode) => void
  parseEditableContent: (raw: unknown) => EditableQuestionContent
}

export function QuestionOptionsEditor({
  selectedQuestionId,
  selectedContent,
  questionType,
  updatePreviewQuestionNode,
  parseEditableContent,
}: QuestionOptionsEditorProps) {
  const t = useTranslations("examHomework")
  return (
    <div className="space-y-2">
      <div className="flex items-center justify-between">
        <Label>{t("exam.questionOptions.label")}</Label>
        <Button
          type="button"
          variant="outline"
          size="sm"
          onClick={() => {
            updatePreviewQuestionNode(selectedQuestionId, (node) => {
              if (!node.question) return node
              const current = parseEditableContent(node.question.content)
              const nextId = String.fromCharCode(65 + current.options.length)
              return {
                ...node,
                question: {
                  ...node.question,
                  content: {
                    ...current,
                    options: [...current.options, { id: nextId, text: "", isCorrect: false }],
                  },
                },
              }
            })
          }}
        >
          <Plus className="mr-1 h-3.5 w-3.5" />
          {t("exam.questionOptions.addOption")}
        </Button>
      </div>
      <div className="space-y-2">
        {selectedContent.options.map((option, optionIndex) => (
          <div key={`${option.id}-${optionIndex}`} className="flex items-center gap-2 rounded-md border p-2">
            <Input
              className="w-16"
              value={option.id}
              onChange={(event) => {
                const nextId = event.target.value
                updatePreviewQuestionNode(selectedQuestionId, (node) => {
                  if (!node.question) return node
                  const current = parseEditableContent(node.question.content)
                  const options = current.options.map((item, idx) => idx === optionIndex ? { ...item, id: nextId } : item)
                  return { ...node, question: { ...node.question, content: { ...current, options } } }
                })
              }}
            />
            <Input
              className="flex-1"
              value={option.text}
              onChange={(event) => {
                const text = event.target.value
                updatePreviewQuestionNode(selectedQuestionId, (node) => {
                  if (!node.question) return node
                  const current = parseEditableContent(node.question.content)
                  const options = current.options.map((item, idx) => idx === optionIndex ? { ...item, text } : item)
                  return { ...node, question: { ...node.question, content: { ...current, options } } }
                })
              }}
            />
            <div className="flex items-center gap-2 px-2">
              <Checkbox
                aria-label={t("exam.questionOptions.markCorrectAria", { id: option.id })}
                checked={option.isCorrect}
                onCheckedChange={(checked) => {
                  const isCorrect = Boolean(checked)
                  updatePreviewQuestionNode(selectedQuestionId, (node) => {
                    if (!node.question) return node
                    const current = parseEditableContent(node.question.content)
                    const options = current.options.map((item, idx) => {
                      if (idx !== optionIndex) {
                        if (questionType === "single_choice") {
                          return { ...item, isCorrect: false }
                        }
                        return item
                      }
                      return { ...item, isCorrect }
                    })
                    return { ...node, question: { ...node.question, content: { ...current, options } } }
                  })
                }}
              />
              <span className="text-xs text-muted-foreground">{t("exam.questionOptions.correct")}</span>
            </div>
            <Button
              type="button"
              variant="ghost"
              size="icon"
              aria-label={t("exam.questionOptions.deleteOptionAria")}
              onClick={() => {
                updatePreviewQuestionNode(selectedQuestionId, (node) => {
                  if (!node.question) return node
                  const current = parseEditableContent(node.question.content)
                  const options = current.options.filter((_, idx) => idx !== optionIndex)
                  return { ...node, question: { ...node.question, content: { ...current, options } } }
                })
              }}
            >
              <Trash2 className="h-3.5 w-3.5" />
            </Button>
          </div>
        ))}
      </div>
    </div>
  )
}
