import type {
  FinanceInvoiceRecordListItemDto,
  FinanceInvoiceSettingDto,
  FinanceInvoiceStatsDto,
} from '#/api/finance';
import type {
  FinanceInvoiceFilterState,
  FinanceInvoicePaginationState,
  FinanceInvoiceSettingFormState,
} from '../../types';

/**
 * 文件职责：发票管理页面数据加载动作。
 */
import {
  getFinanceInvoiceRecordListApi,
  getFinanceInvoiceSettingDetailApi,
} from '#/api/finance';

import {
  applySettingDtoToForm,
  buildInvoiceListQuery,
  cloneSettingsForm,
} from './helpers';

interface DataActionOptions {
  canViewRecords: { value: boolean };
  canViewSettings: { value: boolean };
  createDefaultSettingsForm: () => FinanceInvoiceSettingFormState;
  filters: FinanceInvoiceFilterState;
  isListLoading: { value: boolean };
  isSettingsLoading: { value: boolean };
  pagination: FinanceInvoicePaginationState;
  rows: { value: FinanceInvoiceRecordListItemDto[] };
  settingForm: FinanceInvoiceSettingFormState;
  settingSnapshot: { value: FinanceInvoiceSettingFormState };
  stats: FinanceInvoiceStatsDto;
}

/** 创建页面数据动作。 */
export function createDataActions(options: DataActionOptions) {
  function resetStats() {
    options.stats.currentMonthIssuedAmount = 0;
    options.stats.currentMonthIssuedCount = 0;
    options.stats.pendingCount = 0;
    options.stats.voidedCount = 0;
  }

  function clearRecordData() {
    options.rows.value = [];
    options.pagination.totalCount = 0;
    resetStats();
  }

  function resetSettings() {
    const defaults = options.createDefaultSettingsForm();
    Object.assign(options.settingForm, defaults);
    options.settingSnapshot.value = cloneSettingsForm(defaults);
  }

  function applySettingsResult(result: FinanceInvoiceSettingDto) {
    applySettingDtoToForm(options.settingForm, result);
    options.settingSnapshot.value = cloneSettingsForm(options.settingForm);
  }

  async function loadRecordList() {
    if (!options.canViewRecords.value) {
      clearRecordData();
      return;
    }

    options.isListLoading.value = true;
    try {
      const result = await getFinanceInvoiceRecordListApi(
        buildInvoiceListQuery(options.filters, options.pagination),
      );

      options.rows.value = result.items;
      options.pagination.page = result.page;
      options.pagination.pageSize = result.pageSize;
      options.pagination.totalCount = result.totalCount;

      options.stats.currentMonthIssuedAmount =
        result.stats.currentMonthIssuedAmount;
      options.stats.currentMonthIssuedCount = result.stats.currentMonthIssuedCount;
      options.stats.pendingCount = result.stats.pendingCount;
      options.stats.voidedCount = result.stats.voidedCount;
    } finally {
      options.isListLoading.value = false;
    }
  }

  async function loadSettings() {
    if (!options.canViewSettings.value) {
      resetSettings();
      return;
    }

    options.isSettingsLoading.value = true;
    try {
      const result = await getFinanceInvoiceSettingDetailApi();
      applySettingsResult(result);
    } finally {
      options.isSettingsLoading.value = false;
    }
  }

  function clearByPermission() {
    if (!options.canViewRecords.value) {
      clearRecordData();
    }

    if (!options.canViewSettings.value) {
      resetSettings();
    }
  }

  return {
    applySettingsResult,
    clearByPermission,
    clearRecordData,
    loadRecordList,
    loadSettings,
    resetSettings,
  };
}
