import React, { createContext, useContext, ReactNode } from 'react';
import { usePekTables } from '../hooks/usePekTables';

interface PekTablesContextType {
  // Tables list
  tables: any[];
  isLoadingTables: boolean;
  tablesError: string | null;
  loadTables: () => Promise<void>;

  // Selected table
  selectedTable: string;
  setSelectedTable: (value: string) => void;
  tableData: any[];
  isLoadingTableData: boolean;
  tableDataError: string | null;
  pagination: any;
  loadTableData: (tableName: string, page?: number, pageSize?: number, sort?: Array<{ field: string; direction: 'asc' | 'desc' }>) => Promise<void>;
  refreshTableData: () => void;

  // Commands
  commandInput: string;
  setCommandInput: (value: string) => void;
  processCommand: (userInput: string) => Promise<any>;
  isProcessingCommand: boolean;
  commandError: string | null;
  messages: any[];

  // Create record
  createRecord: (tableName: string, data: any) => Promise<any>;
}

const PekTablesContext = createContext<PekTablesContextType | undefined>(undefined);

export const PekTablesProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const pekTablesData = usePekTables();
  
  return (
    <PekTablesContext.Provider value={pekTablesData}>
      {children}
    </PekTablesContext.Provider>
  );
};

export const usePekTablesContext = (): PekTablesContextType => {
  const context = useContext(PekTablesContext);
  if (!context) {
    throw new Error('usePekTablesContext must be used within a PekTablesProvider');
  }
  return context;
};

