import type { Ref } from 'vue';

import type { StoreListItemDto } from '#/api/store';
import type {
  FlashSaleCardViewModel,
  FlashSaleFilterForm,
  FlashSaleStatsViewModel,
} from '#/views/marketing/flash-sale/types';

/**
 * 文件职责：限时折扣页面数据读取动作。
 */
import { message } from 'ant-design-vue';

import { getMarketingFlashSaleListApi } from '#/api/marketing';
import { getStoreListApi } from '#/api/store';

interface CreateDataActionsOptions {
  filterForm: FlashSaleFilterForm;
  isLoading: Ref<boolean>;
  isStoreLoading: Ref<boolean>;
  keyword: Ref<string>;
  page: Ref<number>;
  pageSize: Ref<number>;
  rows: Ref<FlashSaleCardViewModel[]>;
  selectedStoreId: Ref<string>;
  stats: Ref<FlashSaleStatsViewModel>;
  stores: Ref<StoreListItemDto[]>;
  total: Ref<number>;
}

export function createDataActions(options: CreateDataActionsOptions) {
  async function loadStores() {
    options.isStoreLoading.value = true;
    try {
      const result = await getStoreListApi({
        page: 1,
        pageSize: 200,
      });

      options.stores.value = result.items ?? [];
      if (options.stores.value.length === 0) {
        options.selectedStoreId.value = '';
        options.rows.value = [];
        options.total.value = 0;
        options.stats.value = createEmptyStats();
        return;
      }

      if (!options.selectedStoreId.value) {
        return;
      }

      const hasSelectedStore = options.stores.value.some(
        (item) => item.id === options.selectedStoreId.value,
      );
      if (!hasSelectedStore) {
        options.selectedStoreId.value = '';
      }
    } catch (error) {
      console.error(error);
      message.error('加载门店失败');
    } finally {
      options.isStoreLoading.value = false;
    }
  }

  async function loadActivities() {
    if (options.stores.value.length === 0) {
      options.rows.value = [];
      options.total.value = 0;
      options.stats.value = createEmptyStats();
      return;
    }

    options.isLoading.value = true;
    try {
      const result = await getMarketingFlashSaleListApi({
        storeId: options.selectedStoreId.value || undefined,
        status: options.filterForm.status,
        keyword: options.keyword.value.trim() || undefined,
        page: options.page.value,
        pageSize: options.pageSize.value,
      });

      options.rows.value = result.items ?? [];
      options.total.value = result.total;
      options.page.value = result.page;
      options.pageSize.value = result.pageSize;
      options.stats.value = result.stats;
    } catch (error) {
      console.error(error);
      options.rows.value = [];
      options.total.value = 0;
      options.stats.value = createEmptyStats();
      message.error('加载限时折扣活动失败');
    } finally {
      options.isLoading.value = false;
    }
  }

  return {
    loadActivities,
    loadStores,
  };
}

export function createEmptyStats(): FlashSaleStatsViewModel {
  return {
    totalCount: 0,
    ongoingCount: 0,
    participatingProductCount: 0,
    monthlyDiscountSalesCount: 0,
  };
}
