'use client';

import {dashboardRepository} from '@/features/dashboard/repositories/dashboardRepository';
import type {Granularity} from '@/features/dashboard/types/dashboard';
import {useResource} from '@/shared/hooks/useResource';
import {useScope} from '@/shared/hooks/useScope';
import type {Scope} from '@/shared/services/httpClient';

/**
 * The dashboard's data access, one hook per panel.
 *
 * Components call these and get {status, data, error, refetch}; they never see
 * a URL, a fetch or a scope argument. That is the whole point of the layering
 * — swapping the repository for a GraphQL client, or useResource for TanStack
 * Query, changes these files and nothing that renders.
 *
 * Each panel gets its own hook rather than one `useDashboardData()` returning
 * everything: the store-detail page reuses four of them without pulling in the
 * two it has no room for, and a panel that fails does not blank its neighbours.
 */

export function useDashboardKpis(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.kpis(scope ?? active));
}

export function useDashboardTimeseries(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.timeseries(scope ?? active));
}

export function useDashboardPeakHours(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.peakHours(scope ?? active));
}

export function useDashboardActivity(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.activity(scope ?? active));
}

export function useDashboardRewardUsage(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.rewardUsage(scope ?? active));
}

export function useDashboardStoreComparison(scope?: Scope) {
  const active = useScope();
  return useResource(dashboardRepository.storeComparison(scope ?? active));
}

export function useDashboardPerformance(granularity: Granularity) {
  return useResource(dashboardRepository.performance(useScope(), granularity));
}

export function useDashboardBriefing() {
  return useResource(dashboardRepository.briefing(useScope()));
}
