import React, { createContext, useContext, useState, ReactNode } from 'react';

export interface NetworkMetrics {
  totalSales: number;
  totalProduction: number;
  totalInventoryValue: number;
  averageSales: number;
  averageProduction: number;
  averageInventoryValue: number;
  childCount: number;
}

export interface EnterpriseModeState {
  isNetworkView: boolean;
  selectedOutletId: string | null;
  selectedOutletName: string | null;
  parentTenantId: string | null;
  networkMetrics: NetworkMetrics | null;
  networkViewPath: string | null;
}

interface EnterpriseContextType {
  state: EnterpriseModeState;
  enterNetworkView: (parentTenantId: string) => void;
  drillDownToOutlet: (outletId: string, outletName: string, metrics?: NetworkMetrics) => void;
  returnToNetworkView: () => void;
  updateNetworkMetrics: (metrics: NetworkMetrics) => void;
  clearEnterpriseMode: () => void;
}

const EnterpriseContext = createContext<EnterpriseContextType | undefined>(undefined);

export const useEnterprise = () => {
  const context = useContext(EnterpriseContext);
  if (context === undefined) {
    throw new Error('useEnterprise must be used within an EnterpriseProvider');
  }
  return context;
};

interface EnterpriseProviderProps {
  children: ReactNode;
}

export const EnterpriseProvider: React.FC<EnterpriseProviderProps> = ({ children }) => {
  const [state, setState] = useState<EnterpriseModeState>({
    isNetworkView: false,
    selectedOutletId: null,
    selectedOutletName: null,
    parentTenantId: null,
    networkMetrics: null,
    networkViewPath: null,
  });

  const enterNetworkView = (parentTenantId: string) => {
    setState({
      isNetworkView: true,
      selectedOutletId: null,
      selectedOutletName: null,
      parentTenantId,
      networkMetrics: null,
      networkViewPath: window.location.pathname,
    });
  };

  const drillDownToOutlet = (outletId: string, outletName: string, metrics?: NetworkMetrics) => {
    setState(prev => ({
      ...prev,
      isNetworkView: false,
      selectedOutletId: outletId,
      selectedOutletName: outletName,
      networkMetrics: metrics || prev.networkMetrics,
    }));
  };

  const returnToNetworkView = () => {
    setState(prev => ({
      ...prev,
      isNetworkView: true,
      selectedOutletId: null,
      selectedOutletName: null,
    }));
  };

  const updateNetworkMetrics = (metrics: NetworkMetrics) => {
    setState(prev => ({
      ...prev,
      networkMetrics: metrics,
    }));
  };

  const clearEnterpriseMode = () => {
    setState({
      isNetworkView: false,
      selectedOutletId: null,
      selectedOutletName: null,
      parentTenantId: null,
      networkMetrics: null,
      networkViewPath: null,
    });
  };

  const contextValue: EnterpriseContextType = {
    state,
    enterNetworkView,
    drillDownToOutlet,
    returnToNetworkView,
    updateNetworkMetrics,
    clearEnterpriseMode,
  };

  return (
    <EnterpriseContext.Provider value={contextValue}>
      {children}
    </EnterpriseContext.Provider>
  );
};
