"use client";
import React, { createContext, useContext, useState, ReactNode } from "react";
import WorkflowSetupModal from "./WorkflowSetupModal";

interface WorkflowInfo {
  hasWorkflowSetup: boolean;
  defaultWorkflowId?: number;
  defaultWorkflowName?: string;
  requiresApproval?: boolean;
  autoPublishArticles?: boolean;
  isApprovalActive?: boolean;
}

interface WorkflowModalContextType {
  showWorkflowModal: (workflowInfo: WorkflowInfo) => void;
  hideWorkflowModal: () => void;
}

const WorkflowModalContext = createContext<WorkflowModalContextType | undefined>(undefined);

export function useWorkflowModal() {
  const context = useContext(WorkflowModalContext);
  if (context === undefined) {
    throw new Error('useWorkflowModal must be used within a WorkflowModalProvider');
  }
  return context;
}

interface WorkflowModalProviderProps {
  children: ReactNode;
}

export function WorkflowModalProvider({ children }: WorkflowModalProviderProps) {
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [workflowInfo, setWorkflowInfo] = useState<WorkflowInfo | undefined>(undefined);

  const showWorkflowModal = (info: WorkflowInfo) => {
    setWorkflowInfo(info);
    setIsModalOpen(true);
  };

  const hideWorkflowModal = () => {
    setIsModalOpen(false);
    setWorkflowInfo(undefined);
  };

  return (
    <WorkflowModalContext.Provider value={{ showWorkflowModal, hideWorkflowModal }}>
      {children}
      <WorkflowSetupModal 
        isOpen={isModalOpen} 
        onClose={hideWorkflowModal}
        workflowInfo={workflowInfo}
      />
    </WorkflowModalContext.Provider>
  );
}
