/**
 * useSellerStats Hook
 * TanStack Query hooks for fetching seller statistics and analytics
 */

import { useQuery } from '@tanstack/react-query';
import { marketplaceService } from '../../../services/marketplace.service';
import type {
  SellerStats,
  SellerSalesData,
  SellerPayoutInfo,
} from '../../../types/marketplace.types';

// ============================================================================
// Query Keys
// ============================================================================

export const sellerStatsKeys = {
  all: ['seller', 'stats'] as const,
  overview: () => [...sellerStatsKeys.all, 'overview'] as const,
  sales: (period: string) => [...sellerStatsKeys.all, 'sales', period] as const,
  payouts: () => [...sellerStatsKeys.all, 'payouts'] as const,
  earnings: (period: string) => [...sellerStatsKeys.all, 'earnings', period] as const,
};

// ============================================================================
// Hooks
// ============================================================================

/**
 * Hook for fetching seller's overview statistics
 */
export function useSellerStats() {
  return useQuery<SellerStats, Error>({
    queryKey: sellerStatsKeys.overview(),
    queryFn: () => marketplaceService.getSellerStats(),
    staleTime: 1000 * 60 * 5, // 5 minutes
  });
}

/**
 * Hook for fetching seller's sales data with period filter
 */
export function useSellerSales(period: '7d' | '30d' | '90d' | '1y' = '30d') {
  return useQuery<SellerSalesData[], Error>({
    queryKey: sellerStatsKeys.sales(period),
    queryFn: () => marketplaceService.getSellerSales(period),
    staleTime: 1000 * 60 * 10, // 10 minutes
  });
}

/**
 * Hook for fetching seller's payout information
 */
export function useSellerPayouts() {
  return useQuery<SellerPayoutInfo, Error>({
    queryKey: sellerStatsKeys.payouts(),
    queryFn: () => marketplaceService.getSellerPayouts(),
    staleTime: 1000 * 60 * 5,
  });
}

/**
 * Hook for fetching earnings breakdown by period
 */
export function useSellerEarnings(period: '7d' | '30d' | '90d' | '1y' = '30d') {
  return useQuery<{
    total: number;
    byProduct: { productId: string; productName: string; earnings: number }[];
    trend: number;
  }, Error>({
    queryKey: sellerStatsKeys.earnings(period),
    queryFn: () => marketplaceService.getSellerEarnings(period),
    staleTime: 1000 * 60 * 10,
  });
}
