import type { StoreListItemDto } from '#/api/store';
import type {
  MarketingCalendarMonthCursor,
  MarketingCalendarOverviewViewModel,
} from '#/views/marketing/calendar/types';

/**
 * 文件职责：营销日历页面状态与行为编排。
 */
import { computed, onMounted, ref, watch } from 'vue';

import { useAccessStore } from '@vben/stores';

import {
  CALENDAR_MANAGE_PERMISSION,
  createEmptyOverview,
} from './marketing-calendar-page/constants';
import { createDataActions } from './marketing-calendar-page/data-actions';
import { createDrawerActions } from './marketing-calendar-page/drawer-actions';
import {
  createCurrentMonthCursor,
  formatMonthTitle,
  shiftMonthCursor,
} from './marketing-calendar-page/helpers';

export function useMarketingCalendarPage() {
  const accessStore = useAccessStore();

  const stores = ref<StoreListItemDto[]>([]);
  const selectedStoreId = ref('');
  const isStoreLoading = ref(false);
  const isOverviewLoading = ref(false);

  const cursor = ref<MarketingCalendarMonthCursor>(createCurrentMonthCursor());
  const overview = ref<MarketingCalendarOverviewViewModel>(
    createEmptyOverview(cursor.value.month, cursor.value.year),
  );

  const accessCodeSet = computed(
    () => new Set((accessStore.accessCodes ?? []).map(String)),
  );
  const canManage = computed(() =>
    accessCodeSet.value.has(CALENDAR_MANAGE_PERMISSION),
  );
  const hasStore = computed(() => stores.value.length > 0);
  const storeOptions = computed(() =>
    stores.value.map((item) => ({
      label: item.name,
      value: item.id,
    })),
  );
  const monthTitle = computed(() => formatMonthTitle(cursor.value));

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

  const { loadOverview, loadStores } = createDataActions({
    stores,
    selectedStoreId,
    isStoreLoading,
    isOverviewLoading,
    cursor,
    overview,
  });

  const {
    activeActivity,
    activeConflict,
    activeConflictActivities,
    closeActivityDrawer,
    closeConflictDrawer,
    isActivityDrawerOpen,
    isConflictDrawerOpen,
    openActivityDrawer,
    openConflictDrawer,
    viewActivityFromConflict,
  } = createDrawerActions({
    overview,
  });

  async function prevMonth() {
    cursor.value = shiftMonthCursor(cursor.value, -1);
    await loadOverview();
  }

  async function nextMonth() {
    cursor.value = shiftMonthCursor(cursor.value, 1);
    await loadOverview();
  }

  watch(selectedStoreId, async () => {
    await loadOverview();
  });

  onMounted(async () => {
    await loadStores();
    if (selectedStoreId.value) {
      await loadOverview();
    }
  });

  return {
    activeActivity,
    activeConflict,
    activeConflictActivities,
    canManage,
    closeActivityDrawer,
    closeConflictDrawer,
    hasStore,
    isActivityDrawerOpen,
    isConflictDrawerOpen,
    isOverviewLoading,
    isStoreLoading,
    monthTitle,
    nextMonth,
    openActivityDrawer,
    openConflictDrawer,
    overview,
    prevMonth,
    selectedStoreId,
    setSelectedStoreId,
    storeOptions,
    viewActivityFromConflict,
  };
}
