"use client";
import { useEffect, useState } from "react";

interface PathConfigEditorProps {
  value: any;
  onChange: (value: any) => void;
}

export default function PathConfigEditor({
  value,
  onChange
}: PathConfigEditorProps) {
  const [useJsonMode, setUseJsonMode] = useState(false);
  const [jsonValue, setJsonValue] = useState("");
  const [formData, setFormData] = useState<Record<string, any>>({});
  const [handbrakePresets, setHandbrakePresets] = useState<string[]>([]);
  const [extensions, setExtensions] = useState<string[]>([]);

  // Fetch handbrake presets on mount
  useEffect(() => {
    fetch("/api/handbrake/presets")
      .then((res) => res.json())
      .then((data) => {
        if (Array.isArray(data)) {
          setHandbrakePresets(data);
        }
      })
      .catch((err) => console.error("Failed to fetch handbrake presets:", err));
  }, []);

  // Fetch extensions from settings
  useEffect(() => {
    fetch("/api/config/settings/extensions")
      .then((res) => res.json())
      .then((data) => {
        if (Array.isArray(data)) {
          setExtensions(data);
        }
      })
      .catch((err) => console.error("Failed to fetch extensions:", err));
  }, []);

  // Initialize form data - default to form mode
  useEffect(() => {
    setUseJsonMode(false);
    if (value && typeof value === "object" && !Array.isArray(value)) {
      setFormData({ ...value });
    } else {
      setFormData({});
    }
    setJsonValue(JSON.stringify(value || {}, null, 2));
  }, [value]);

  const handleJsonChange = (newJson: string) => {
    setJsonValue(newJson);
    try {
      const parsed = JSON.parse(newJson);
      onChange(parsed);
    } catch {
      // Invalid JSON, don't update parent yet
    }
  };

  const handleFormFieldChange = (key: string, fieldValue: any) => {
    const newFormData = { ...formData, [key]: fieldValue };
    setFormData(newFormData);
    onChange(newFormData);
  };

  const addFormField = () => {
    const newKey = `field_${Object.keys(formData).length + 1}`;
    handleFormFieldChange(newKey, "");
  };

  const removeFormField = (key: string) => {
    const newFormData = { ...formData };
    delete newFormData[key];
    setFormData(newFormData);
    onChange(newFormData);
  };

  const switchToJsonMode = () => {
    setUseJsonMode(true);
    setJsonValue(JSON.stringify(formData, null, 2));
    onChange(formData);
  };

  const switchToFormMode = () => {
    try {
      const parsed = JSON.parse(jsonValue);
      if (typeof parsed === "object" && !Array.isArray(parsed)) {
        setUseJsonMode(false);
        setFormData(parsed);
        onChange(parsed);
        return;
      }
      // If we can't convert to form mode, stay in JSON mode
    } catch {
      // Invalid JSON, stay in JSON mode
    }
  };

  // Render specific field types
  const renderFieldInput = (key: string, val: any) => {
    switch (key) {
      case "exts":
        return (
          <div className="flex-1">
            <select
              multiple
              value={Array.isArray(val) ? val : []}
              onChange={(e) => {
                const selected = Array.from(
                  e.target.selectedOptions,
                  (option) => option.value
                );
                handleFormFieldChange(key, selected);
              }}
              className="w-full rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs h-24"
            >
              {extensions.map((ext) => (
                <option key={ext} value={ext}>
                  {ext}
                </option>
              ))}
            </select>
            <p className="text-xs text-gray-500 mt-1">
              Hold Ctrl/Cmd to select multiple
            </p>
          </div>
        );

      case "ext":
        return (
          <select
            value={val || ""}
            onChange={(e) => handleFormFieldChange(key, e.target.value)}
            className="flex-1 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs"
          >
            <option value="">Select extension...</option>
            {extensions.map((ext) => (
              <option key={ext} value={ext}>
                {ext}
              </option>
            ))}
          </select>
        );

      case "preset":
        return (
          <select
            value={val || ""}
            onChange={(e) => handleFormFieldChange(key, e.target.value)}
            className="flex-1 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs"
          >
            <option value="">Select preset...</option>
            {handbrakePresets.map((preset) => (
              <option key={preset} value={preset}>
                {preset}
              </option>
            ))}
          </select>
        );

      case "clean":
        return (
          <div className="flex-1">
            <textarea
              value={
                typeof val === "object"
                  ? JSON.stringify(val, null, 2)
                  : val || ""
              }
              onChange={(e) => {
                try {
                  const parsed = JSON.parse(e.target.value);
                  handleFormFieldChange(key, parsed);
                } catch {
                  handleFormFieldChange(key, e.target.value);
                }
              }}
              placeholder='{"regex": "replacement"}'
              rows={6}
              className="w-full rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 font-mono text-xs"
            />
            <p className="text-xs text-gray-500 mt-1">
              JSON object with regex patterns
            </p>
          </div>
        );

      default:
        if (typeof val === "boolean") {
          return (
            <div className="flex items-center gap-2 flex-1">
              <input
                type="checkbox"
                checked={val}
                onChange={(e) => handleFormFieldChange(key, e.target.checked)}
                className="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded"
              />
              <span className="text-xs text-gray-600 dark:text-gray-400">
                Boolean
              </span>
            </div>
          );
        } else if (typeof val === "number") {
          return (
            <input
              type="number"
              value={val}
              onChange={(e) =>
                handleFormFieldChange(key, parseFloat(e.target.value) || 0)
              }
              placeholder="Number value"
              className="flex-1 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs"
            />
          );
        } else {
          return (
            <input
              type="text"
              value={val || ""}
              onChange={(e) => handleFormFieldChange(key, e.target.value)}
              placeholder="String value"
              className="flex-1 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs"
            />
          );
        }
    }
  };

  if (useJsonMode) {
    return (
      <div className="space-y-3">
        <div className="flex items-center justify-between">
          <label className="block text-sm font-medium text-gray-700 dark:text-gray-200">
            Configuration (JSON)
          </label>
          <button
            type="button"
            onClick={switchToFormMode}
            className="text-xs text-indigo-600 hover:text-indigo-800 dark:text-indigo-400 dark:hover:text-indigo-200"
          >
            Switch to Form
          </button>
        </div>
        <textarea
          value={jsonValue}
          onChange={(e) => handleJsonChange(e.target.value)}
          placeholder='{"key": "value"}'
          rows={6}
          className="w-full rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 font-mono text-xs"
        />
        {(() => {
          try {
            JSON.parse(jsonValue);
            return null;
          } catch {
            return (
              <p className="text-xs text-red-600 dark:text-red-400">
                Invalid JSON format
              </p>
            );
          }
        })()}
      </div>
    );
  }

  return (
    <div className="space-y-3">
      <div className="flex items-center justify-between">
        <label className="block text-sm font-medium text-gray-700 dark:text-gray-200">
          Configuration (Form)
        </label>
        <button
          type="button"
          onClick={switchToJsonMode}
          className="text-xs text-indigo-600 hover:text-indigo-800 dark:text-indigo-400 dark:hover:text-indigo-200"
        >
          Switch to JSON
        </button>
      </div>

      {Object.keys(formData).length === 0 ? (
        <p className="text-sm text-gray-500 italic text-center py-4">
          No configuration fields. Add a field or switch to JSON mode.
        </p>
      ) : (
        <div className="space-y-3">
          {Object.entries(formData).map(([key, val]) => (
            <div key={key} className="flex gap-2 items-start">
              <input
                type="text"
                value={key}
                onChange={(e) => {
                  const newFormData = { ...formData };
                  delete newFormData[key];
                  newFormData[e.target.value] = val;
                  setFormData(newFormData);
                  onChange(newFormData);
                }}
                placeholder="Field name"
                className="w-1/3 rounded-md border-gray-300 dark:border-gray-700 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100 text-xs"
              />

              {renderFieldInput(key, val)}

              <button
                type="button"
                onClick={() => removeFormField(key)}
                className="inline-flex items-center rounded-md bg-red-600 px-2 py-1 text-xs font-medium text-white shadow-sm hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2"
              >
                ×
              </button>
            </div>
          ))}
        </div>
      )}

      <button
        type="button"
        onClick={addFormField}
        className="text-xs text-green-600 hover:text-green-800 dark:text-green-400 dark:hover:text-green-200"
      >
        + Add Field
      </button>
    </div>
  );
}
