import { createContext, useContext, ReactNode } from "react";
import { useAuth } from "./AuthContext";

interface TenantContextType {
  tenantId: number | null;
  tenantName: string | null;
}

const TenantContext = createContext<TenantContextType | undefined>(undefined);

export function TenantProvider({ children }: { children: ReactNode }) {
  const { user } = useAuth();

  return (
    <TenantContext.Provider
      value={{
        tenantId: user?.tenantId ?? null,
        tenantName: user?.tenantName ?? null,
      }}
    >
      {children}
    </TenantContext.Provider>
  );
}

export function useTenant() {
  const context = useContext(TenantContext);
  if (context === undefined) {
    throw new Error("useTenant must be used within a TenantProvider");
  }
  return context;
}
