import React, { useEffect } from "react";
import { TaskDefinition } from "../../../workflowTemplates/services/WorkflowTemplateService";
import { GetAssignmentForCompletion } from "../services/assignmentCompleteService";
import BasicTask from "./tasksProcessor/BasicTask";

type TaskProcessorProps = {
  assignmentDetails: GetAssignmentForCompletion;
  taskDefinition: TaskDefinition;
  taskData: Record<string, unknown>;
  onTaskDataChange?: (data: Record<string, unknown>) => void;
  onValidationChange?: (isValid: boolean) => void;
};

type TaskProcessorComponent = React.ComponentType<TaskProcessorProps>;

const taskProcessorRegistry: Record<string, TaskProcessorComponent> = {
  "e_suite.Workflow.Core.Tasks.BasicTask": BasicTask,
};

const TaskProcessor: React.FC<TaskProcessorProps> = ({
  assignmentDetails,
  taskDefinition,
  taskData,
  onTaskDataChange,
  onValidationChange,
}) => {
  const processorType = assignmentDetails.task.taskType;
  const Processor = taskProcessorRegistry[processorType];

  useEffect(() => {
    if (!Processor) {
      onValidationChange?.(false);
    }
  }, [Processor, onValidationChange]);

  if (!Processor) {
    return <div>Unsupported task type: {processorType}</div>;
  }

  return (
    <Processor
      assignmentDetails={assignmentDetails}
      taskDefinition={taskDefinition}
      taskData={taskData}
      onTaskDataChange={onTaskDataChange}
      onValidationChange={onValidationChange}
    />
  );
};

export default TaskProcessor;
