"use client";

import React, { useState, useEffect } from "react";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
  DialogDescription,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Checkbox } from "@/components/ui/checkbox";
import { TableCategoryValue } from "@/types/tableCategoryValue";

interface CategoryValueEditDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  value: TableCategoryValue;
  onUpdate: (valueId: number, updates: Partial<TableCategoryValue>) => void;
  columnLabel: string;
}

export const CategoryValueEditDialog: React.FC<
  CategoryValueEditDialogProps
> = ({ open, onOpenChange, value, onUpdate, columnLabel }) => {
  const [valueLabel, setValueLabel] = useState(value.valueLabel);
  const [description, setDescription] = useState(value.description || "");
  const [color, setColor] = useState(value.color || "#3b82f6");
  const [isDefault, setIsDefault] = useState(value.isDefault || false);
  const [isActive, setIsActive] = useState(value.isActive !== false);

  useEffect(() => {
    setValueLabel(value.valueLabel);
    setDescription(value.description || "");
    setColor(value.color || "#3b82f6");
    setIsDefault(value.isDefault || false);
    setIsActive(value.isActive !== false);
  }, [value]);

  const handleSubmit = () => {
    if (!valueLabel) {
      return;
    }

    onUpdate(value.valueId!, {
      valueLabel,
      description,
      color,
      isDefault,
      isActive,
    });
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-[95vw] sm:max-w-[500px]">
        <DialogHeader>
          <DialogTitle className="text-base sm:text-lg">
            카테고리 값 편집
          </DialogTitle>
          <DialogDescription className="text-xs sm:text-sm">
            {columnLabel} - {value.valueCode}
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-3 sm:space-y-4">
          <div>
            <Label htmlFor="valueCode" className="text-xs sm:text-sm">
              코드
            </Label>
            <Input
              id="valueCode"
              value={value.valueCode}
              disabled
              className="h-8 text-xs sm:h-10 sm:text-sm"
            />
            <p className="text-muted-foreground mt-1 text-[10px] sm:text-xs">
              코드는 변경할 수 없습니다
            </p>
          </div>

          <div>
            <Label htmlFor="valueLabel" className="text-xs sm:text-sm">
              라벨 *
            </Label>
            <Input
              id="valueLabel"
              value={valueLabel}
              onChange={(e) => setValueLabel(e.target.value)}
              className="h-8 text-xs sm:h-10 sm:text-sm"
            />
          </div>

          <div>
            <Label htmlFor="description" className="text-xs sm:text-sm">
              설명
            </Label>
            <Textarea
              id="description"
              value={description}
              onChange={(e) => setDescription(e.target.value)}
              className="text-xs sm:text-sm"
              rows={3}
            />
          </div>

          <div>
            <Label htmlFor="color" className="text-xs sm:text-sm">
              색상
            </Label>
            <div className="flex gap-2">
              <Input
                id="color"
                type="color"
                value={color}
                onChange={(e) => setColor(e.target.value)}
                className="h-8 w-16 sm:h-10"
              />
              <Input
                type="text"
                value={color}
                onChange={(e) => setColor(e.target.value)}
                className="h-8 flex-1 text-xs sm:h-10 sm:text-sm"
              />
            </div>
          </div>

          <div className="flex items-center gap-2">
            <Checkbox
              id="isDefault"
              checked={isDefault}
              onCheckedChange={(checked) => setIsDefault(checked as boolean)}
            />
            <Label htmlFor="isDefault" className="text-xs sm:text-sm">
              기본값으로 설정
            </Label>
          </div>

          <div className="flex items-center gap-2">
            <Checkbox
              id="isActive"
              checked={isActive}
              onCheckedChange={(checked) => setIsActive(checked as boolean)}
            />
            <Label htmlFor="isActive" className="text-xs sm:text-sm">
              활성화
            </Label>
          </div>
        </div>

        <DialogFooter className="gap-2 sm:gap-0">
          <Button
            variant="outline"
            onClick={() => onOpenChange(false)}
            className="h-8 flex-1 text-xs sm:h-10 sm:flex-none sm:text-sm"
          >
            취소
          </Button>
          <Button
            onClick={handleSubmit}
            disabled={!valueLabel}
            className="h-8 flex-1 text-xs sm:h-10 sm:flex-none sm:text-sm"
          >
            저장
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};

