import { Input, Select, ConditionalCheckboxBlock, RadioGroup, Checkbox } from "../FormsFieldsTypes";

interface StatutFieldsProps {
  statutId: number;
  field: any;
  refData: Record<string, any>;
  situations: Record<number, any>;
  updateSituationField: (id: number, field: string, value: any) => void;
}

export function StatutFieldsRenderer({
  statutId,
  field,
  refData,
  situations,
  updateSituationField
}: StatutFieldsProps) {
  const currentFields = situations[statutId]?.fields || {};
  const currentValue = currentFields[field.name];

  /**
   * ✅ GESTION showIf
   */

  if (!isFieldVisible(field.showIf, currentFields)) {
    return null;
  }

  /**
   * ✅ OPTIONS (direct ou refData)
   */
  const options =
    field.options ||
    refData[field.optionsKey]?.data ||
    [];

  switch (field.type) {
    case "input":
      return (
        <Input
          label={field.label}
          name={field.name}
          value={currentValue || ""}
          onChange={(name: string, value: any) =>
            updateSituationField(statutId, name, value)
          }
        />
      );

    case "date":
      return (
        <Input
          type="date"
          label={field.label}
          name={field.name}
          value={currentValue || ""}
          onChange={(name: string, value: any) =>
            updateSituationField(statutId, name, value)
          }
        />
      );

    case "select":
      return (
        <Select
          label={field.label}
          name={field.name}
          options={options}
          value={currentValue || ""}
          onChange={(name: string, value: any) =>
            updateSituationField(statutId, name, value)
          }
        />
      );
    case "radio":
      return (
        <RadioGroup
          label={field.label}
          name={field.name}
          value={currentValue || ""}
          options={options}
          onChange={(name, value) =>
            updateSituationField(statutId, name, value)
          }
        />
      );

    case "checkbox":
      return (
        <Checkbox
          label={field.label}
          checked={!!currentValue}
          onChange={() => updateSituationField(
            statutId,
            field.name,
            !currentValue
          )}
          name={field.name}
          value={currentValue}
        />
      );

    case "nestedCheckbox":
      const nestedChecked = currentValue?.checked || false;

      return (
        <ConditionalCheckboxBlock
          label={field.label}
          name={field.name}
          value={field.name}
          checked={nestedChecked}
          onToggle={() =>
            updateSituationField(statutId, field.name, {
              ...currentValue,
              checked: !nestedChecked
            })
          }
        >
          <div className="grid grid-cols-2 gap-4 w-full">
            {field.children?.map((child: any) => (
              <StatutFieldsRenderer
                key={child.name}
                statutId={statutId}
                field={child}
                refData={refData}
                situations={situations}
                updateSituationField={updateSituationField}
              />
            ))}
          </div>
        </ConditionalCheckboxBlock>
      );

    default:
      return null;
  }
}


function isFieldVisible(showIf: any, currentFields: Record<string, any>) {
  if (!showIf) return true;

  const targetValue = currentFields[showIf.field];

  if (targetValue === null || targetValue === undefined) {
    return false;
  }

  if (typeof targetValue === "object" && "checked" in targetValue) {
    return (targetValue.checked === showIf.equals);
  }

  return targetValue === showIf.equals;
}