"use client";

import React from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Trash2 } from "lucide-react";
import { ColumnInfo } from "@/lib/api/dataflow";
import { DataSaveSettings } from "@/types/connectionTypes";

interface ActionSplitConfigProps {
  action: DataSaveSettings["actions"][0];
  actionIndex: number;
  settings: DataSaveSettings;
  onSettingsChange: (settings: DataSaveSettings) => void;
  fromTableColumns: ColumnInfo[];
  toTableColumns: ColumnInfo[];
}

export const ActionSplitConfig: React.FC<ActionSplitConfigProps> = ({
  action,
  actionIndex,
  settings,
  onSettingsChange,
  fromTableColumns,
  toTableColumns,
}) => {
  const updateSplitConfig = (field: string, value: string) => {
    const newActions = [...settings.actions];
    if (!newActions[actionIndex].splitConfig) {
      newActions[actionIndex].splitConfig = {
        sourceField: "",
        delimiter: ",",
        targetField: "",
      };
    }
    (newActions[actionIndex].splitConfig as any)[field] = value;
    onSettingsChange({ ...settings, actions: newActions });
  };

  const clearSplitConfig = () => {
    const newActions = [...settings.actions];
    newActions[actionIndex].splitConfig = {
      sourceField: "",
      delimiter: ",",
      targetField: "",
    };
    onSettingsChange({ ...settings, actions: newActions });
  };

  return (
    <div className="mt-3">
      <details className="group">
        <summary className="flex cursor-pointer items-center justify-between rounded border p-2 text-xs font-medium text-foreground hover:bg-muted hover:text-foreground">
          <div className="flex items-center gap-2">
            ✂️ 데이터 분할 설정 (선택사항)
            {action.splitConfig && action.splitConfig.sourceField && (
              <span className="rounded-full bg-emerald-100 px-2 py-0.5 text-xs text-emerald-700">설정됨</span>
            )}
          </div>
          {action.splitConfig && action.splitConfig.sourceField && (
            <Button
              size="sm"
              variant="ghost"
              onClick={(e) => {
                e.preventDefault();
                e.stopPropagation();
                clearSplitConfig();
              }}
              className="h-5 w-5 p-0 text-destructive hover:text-destructive"
              title="분할 설정 초기화"
            >
              <Trash2 className="h-3 w-3" />
            </Button>
          )}
        </summary>
        <div className="mt-2 space-y-2 border-l-2 border-border pl-4">
          <Label className="text-xs font-medium">데이터 분할 설정</Label>
          <div className="mt-1 grid grid-cols-3 gap-2">
            <div>
              <Label className="text-xs text-muted-foreground">분할할 필드</Label>
              <Select
                value={action.splitConfig?.sourceField || ""}
                onValueChange={(value) => updateSplitConfig("sourceField", value)}
              >
                <SelectTrigger className="h-6 text-xs">
                  <SelectValue placeholder="필드 선택" />
                </SelectTrigger>
                <SelectContent>
                  {fromTableColumns.map((column) => (
                    <SelectItem key={column.columnName} value={column.columnName}>
                      {column.displayName && column.displayName !== column.columnName
                        ? column.displayName
                        : column.columnName}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div>
              <Label className="text-xs text-muted-foreground">구분자</Label>
              <Input
                value={action.splitConfig?.delimiter || ""}
                onChange={(e) => updateSplitConfig("delimiter", e.target.value)}
                className="h-6 text-xs"
                placeholder=","
              />
            </div>
            <div>
              <Label className="text-xs text-muted-foreground">저장할 필드</Label>
              <Select
                value={action.splitConfig?.targetField || ""}
                onValueChange={(value) => updateSplitConfig("targetField", value)}
              >
                <SelectTrigger className="h-6 text-xs">
                  <SelectValue placeholder="필드 선택" />
                </SelectTrigger>
                <SelectContent>
                  {toTableColumns.map((column) => (
                    <SelectItem key={column.columnName} value={column.columnName}>
                      {column.displayName && column.displayName !== column.columnName
                        ? column.displayName
                        : column.columnName}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
          </div>
        </div>
      </details>
    </div>
  );
};
