import type { OrderAllDetailDto, OrderAllListItemDto } from '#/api/order';
import type { StoreListItemDto } from '#/api/store';

import { computed, onActivated, onMounted, reactive, ref, watch } from 'vue';

import {
  createDefaultFilters,
  DEFAULT_STATS,
} from './order-all-page/constants';
import { createDataActions } from './order-all-page/data-actions';
import { createDrawerActions } from './order-all-page/drawer-actions';
import { createExportActions } from './order-all-page/export-actions';
import { createFilterActions } from './order-all-page/filter-actions';

/**
 * 文件职责：全部订单页面状态与动作编排。
 */
export function useOrderAllPage() {
  const stores = ref<StoreListItemDto[]>([]);
  const selectedStoreId = ref('');
  const isStoreLoading = ref(false);

  const filters = reactive(createDefaultFilters());
  const rows = ref<OrderAllListItemDto[]>([]);
  const pagination = reactive({
    page: 1,
    pageSize: 10,
    total: 0,
  });

  const stats = reactive({ ...DEFAULT_STATS });
  const isListLoading = ref(false);
  const isStatsLoading = ref(false);

  const detail = ref<null | OrderAllDetailDto>(null);
  const isDrawerOpen = ref(false);
  const isDetailLoading = ref(false);
  const isExporting = ref(false);

  const storeOptions = computed(() =>
    stores.value.map((item) => ({
      label: item.name,
      value: item.id,
    })),
  );

  const { loadPageData, loadStores } = createDataActions({
    stores,
    selectedStoreId,
    filters,
    rows,
    pagination,
    stats,
    isStoreLoading,
    isListLoading,
    isStatsLoading,
  });

  const {
    handlePageChange,
    handleReset,
    handleSearch,
    setChannel,
    setEndDate,
    setKeyword,
    setPaymentMethod,
    setStartDate,
    setStatus,
  } = createFilterActions({
    filters,
    pagination,
    loadPageData,
  });

  const { openDetail, setDrawerOpen } = createDrawerActions({
    selectedStoreId,
    detail,
    isDrawerOpen,
    isDetailLoading,
  });

  const { handleExport } = createExportActions({
    selectedStoreId,
    filters,
    isExporting,
  });

  function setSelectedStoreId(value: string) {
    selectedStoreId.value = value;
  }

  watch(selectedStoreId, async (storeId) => {
    if (!storeId) {
      rows.value = [];
      pagination.total = 0;
      stats.totalOrders = DEFAULT_STATS.totalOrders;
      stats.totalAmount = DEFAULT_STATS.totalAmount;
      stats.averageAmount = DEFAULT_STATS.averageAmount;
      stats.refundCount = DEFAULT_STATS.refundCount;
      detail.value = null;
      return;
    }

    pagination.page = 1;
    await loadPageData();
  });

  onMounted(() => {
    void loadStores();
  });

  onActivated(() => {
    if (stores.value.length === 0 || !selectedStoreId.value) {
      void loadStores();
    }
  });

  return {
    detail,
    filters,
    handleExport,
    handlePageChange,
    handleReset,
    handleSearch,
    isDetailLoading,
    isDrawerOpen,
    isExporting,
    isListLoading,
    isStatsLoading,
    isStoreLoading,
    openDetail,
    pagination,
    rows,
    selectedStoreId,
    setChannel,
    setDrawerOpen,
    setEndDate,
    setKeyword,
    setPaymentMethod,
    setSelectedStoreId,
    setStartDate,
    setStatus,
    stats,
    storeOptions,
  };
}
