/**
 * 网站数据组合式函数
 * 提供统一的网站信息和统计数据访问接口
 */
import { computed } from 'vue';
import { useSiteStore } from '@/store/modules/site';
import { useStatisticsStore } from '@/store/modules/statistics';

export function useSiteData() {
  const siteStore = useSiteStore();
  const statisticsStore = useStatisticsStore();

  // 网站信息相关
  const siteInfo = computed(() => siteStore.siteInfo);
  const websiteName = computed(() => siteStore.appName);
  const websiteLogo = computed(() => siteStore.logo);
  const websiteComments = computed(() => siteStore.description);
  const websiteDarkLogo = computed(() => siteStore.logo);
  const websiteDomain = computed(() => siteStore.domain);
  const websiteId = computed(() => siteStore.appId);
  const runDays = computed(() => siteStore.runDays);
  const siteLoading = computed(() => siteStore.loading);

  // 统计数据相关
  const statistics = computed(() => statisticsStore.statistics);
  const userCount = computed(() => statisticsStore.userCount);
  const orderCount = computed(() => statisticsStore.orderCount);
  const totalSales = computed(() => statisticsStore.totalSales);
  const todaySales = computed(() => statisticsStore.todaySales);
  const monthSales = computed(() => statisticsStore.monthSales);
  const todayOrders = computed(() => statisticsStore.todayOrders);
  const todayUsers = computed(() => statisticsStore.todayUsers);
  const statisticsLoading = computed(() => statisticsStore.loading);

  // 整体加载状态
  const loading = computed(() => siteLoading.value || statisticsLoading.value);

  // 方法
  const fetchSiteInfo = (forceRefresh = false) => {
    return siteStore.fetchSiteInfo(forceRefresh);
  };

  const fetchStatistics = (forceRefresh = false) => {
    return statisticsStore.fetchStatistics(forceRefresh);
  };

  const refreshAll = async (forceRefresh = true) => {
    await Promise.all([
      fetchSiteInfo(forceRefresh),
      fetchStatistics(forceRefresh)
    ]);
  };

  const startAutoRefresh = (interval?: number) => {
    statisticsStore.startAutoRefresh(interval);
  };

  const stopAutoRefresh = () => {
    statisticsStore.stopAutoRefresh();
  };

  const clearCache = () => {
    siteStore.clearCache();
    statisticsStore.clearCache();
  };

  return {
    // 网站信息
    siteInfo,
    websiteName,
    websiteLogo,
    websiteComments,
    websiteDarkLogo,
    websiteDomain,
    websiteId,
    runDays,
    siteLoading,

    // 统计数据
    statistics,
    userCount,
    orderCount,
    totalSales,
    todaySales,
    monthSales,
    todayOrders,
    todayUsers,
    statisticsLoading,

    // 状态
    loading,

    // 方法
    fetchSiteInfo,
    fetchStatistics,
    refreshAll,
    startAutoRefresh,
    stopAutoRefresh,
    clearCache
  };
}
