/**
 * Enterprise Dashboard Hooks - Clean Implementation
 *
 * Phase 3 Complete: All dashboard hooks call services directly.
 * Distribution and forecast still use orchestrator (Phase 4 migration).
 */

export {
  useNetworkSummary,
  useChildrenPerformance,
  useChildTenants,
  useChildSales,
  useChildInventory,
  useChildProduction,
} from './useEnterpriseDashboard';

export type {
  NetworkSummary,
  PerformanceRankings as ChildPerformance,
  ChildTenant,
  SalesSummary,
  InventorySummary,
  ProductionSummary,
} from './useEnterpriseDashboard';

// Distribution and forecast hooks (Phase 4 - To be migrated)
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { ApiError, apiClient } from '../client';

export interface DistributionOverview {
    route_sequences: any[];
    status_counts: {
        pending: number;
        in_transit: number;
        delivered: number;
        failed: number;
        [key: string]: number;
    };
}

export interface ForecastSummary {
    aggregated_forecasts: Record<string, any>;
    days_forecast: number;
    last_updated: string;
}

export const useDistributionOverview = (
    tenantId: string,
    targetDate?: string,
    options?: Omit<UseQueryOptions<DistributionOverview, ApiError>, 'queryKey' | 'queryFn'>
) => {
    return useQuery<DistributionOverview, ApiError>({
        queryKey: ['enterprise', 'distribution-overview', tenantId, targetDate],
        queryFn: async () => {
            const params = new URLSearchParams();
            if (targetDate) params.append('target_date', targetDate);
            const queryString = params.toString();
            return apiClient.get<DistributionOverview>(
                `/tenants/${tenantId}/enterprise/distribution-overview${queryString ? `?${queryString}` : ''}`
            );
        },
        enabled: !!tenantId,
        staleTime: 30000,
        ...options,
    });
};

export const useForecastSummary = (
    tenantId: string,
    daysAhead: number = 7,
    options?: Omit<UseQueryOptions<ForecastSummary, ApiError>, 'queryKey' | 'queryFn'>
) => {
    return useQuery<ForecastSummary, ApiError>({
        queryKey: ['enterprise', 'forecast-summary', tenantId, daysAhead],
        queryFn: async () => {
            return apiClient.get<ForecastSummary>(
                `/tenants/${tenantId}/enterprise/forecast-summary?days_ahead=${daysAhead}`
            );
        },
        enabled: !!tenantId,
        staleTime: 120000,
        ...options,
    });
};
