import type { Ref } from 'vue';

import type {
  PointMallRecordDetailViewModel,
  PointMallVerifyForm,
} from '#/views/member/points-mall/types';

import { message } from 'ant-design-vue';

import {
  getMemberPointMallRecordDetailApi,
  verifyMemberPointMallRecordApi,
} from '#/api/member/points-mall';

interface CreateRecordActionsOptions {
  canManage: Ref<boolean>;
  detailRecord: Ref<null | PointMallRecordDetailViewModel>;
  isDetailLoading: Ref<boolean>;
  isDetailOpen: Ref<boolean>;
  isVerifyLoading: Ref<boolean>;
  isVerifyOpen: Ref<boolean>;
  isVerifySubmitting: Ref<boolean>;
  loadRecordList: () => Promise<void>;
  selectedRecordId: Ref<string>;
  selectedStoreId: Ref<string>;
  verifyForm: PointMallVerifyForm;
}

export function createRecordActions(options: CreateRecordActionsOptions) {
  async function loadRecordDetail(recordId: string) {
    if (!options.selectedStoreId.value || !recordId) {
      options.detailRecord.value = null;
      return;
    }

    const detail = await getMemberPointMallRecordDetailApi({
      storeId: options.selectedStoreId.value,
      recordId,
    });
    options.detailRecord.value = detail;
  }

  async function openDetailDrawer(recordId: string) {
    if (!options.selectedStoreId.value) {
      message.warning('请先选择门店');
      return;
    }

    options.selectedRecordId.value = recordId;
    options.isDetailLoading.value = true;
    options.isDetailOpen.value = true;

    try {
      await loadRecordDetail(recordId);
    } catch (error) {
      console.error(error);
      options.isDetailOpen.value = false;
      message.error('加载记录详情失败');
    } finally {
      options.isDetailLoading.value = false;
    }
  }

  async function openVerifyDrawer(recordId: string) {
    if (!options.canManage.value) {
      return;
    }

    if (!options.selectedStoreId.value) {
      message.warning('请先选择门店');
      return;
    }

    options.selectedRecordId.value = recordId;
    options.isVerifyLoading.value = true;
    options.isVerifyOpen.value = true;
    options.verifyForm.verifyMethod = 'manual';
    options.verifyForm.verifyRemark = '';

    try {
      await loadRecordDetail(recordId);
    } catch (error) {
      console.error(error);
      options.isVerifyOpen.value = false;
      message.error('加载核销信息失败');
    } finally {
      options.isVerifyLoading.value = false;
    }
  }

  function setDetailOpen(value: boolean) {
    options.isDetailOpen.value = value;
    if (!value) {
      options.isDetailLoading.value = false;
    }
  }

  function setVerifyOpen(value: boolean) {
    options.isVerifyOpen.value = value;
    if (!value) {
      options.isVerifyLoading.value = false;
      options.isVerifySubmitting.value = false;
    }
  }

  function setVerifyMethod(value: PointMallVerifyForm['verifyMethod']) {
    options.verifyForm.verifyMethod = value;
  }

  function setVerifyRemark(value: string) {
    options.verifyForm.verifyRemark = value;
  }

  async function submitVerify() {
    if (!options.canManage.value) {
      return;
    }

    if (!options.selectedStoreId.value) {
      message.warning('请先选择门店');
      return;
    }

    if (!options.selectedRecordId.value) {
      message.warning('请选择需要核销的记录');
      return;
    }

    options.isVerifySubmitting.value = true;
    try {
      await verifyMemberPointMallRecordApi({
        storeId: options.selectedStoreId.value,
        recordId: options.selectedRecordId.value,
        verifyMethod: options.verifyForm.verifyMethod,
        verifyRemark: options.verifyForm.verifyRemark.trim() || undefined,
      });
      message.success('核销成功');
      options.isVerifyOpen.value = false;
      await options.loadRecordList();
      await loadRecordDetail(options.selectedRecordId.value);
    } catch (error) {
      console.error(error);
      message.error('核销失败');
    } finally {
      options.isVerifySubmitting.value = false;
    }
  }

  return {
    openDetailDrawer,
    openVerifyDrawer,
    setDetailOpen,
    setVerifyMethod,
    setVerifyOpen,
    setVerifyRemark,
    submitVerify,
  };
}
