/**
 * 文件职责：财务中心发票管理 API 契约与请求封装。
 */
import { requestClient } from '#/api/request';

/** 发票状态筛选值。 */
export type FinanceInvoiceStatusFilter = 'all' | 'issued' | 'pending' | 'voided';

/** 发票类型筛选值。 */
export type FinanceInvoiceTypeFilter = 'all' | 'normal' | 'special';

/** 发票设置详情。 */
export interface FinanceInvoiceSettingDto {
  autoIssueMaxAmount: number;
  bankAccount?: string;
  bankName?: string;
  companyName: string;
  enableAutoIssue: boolean;
  enableElectronicNormalInvoice: boolean;
  enableElectronicSpecialInvoice: boolean;
  registeredAddress?: string;
  registeredPhone?: string;
  taxpayerNumber: string;
}

/** 保存发票设置请求。 */
export interface SaveFinanceInvoiceSettingPayload
  extends FinanceInvoiceSettingDto {}

/** 发票记录列表查询参数。 */
export interface FinanceInvoiceRecordListQuery {
  endDate?: string;
  invoiceType?: Exclude<FinanceInvoiceTypeFilter, 'all'>;
  keyword?: string;
  page: number;
  pageSize: number;
  startDate?: string;
  status?: Exclude<FinanceInvoiceStatusFilter, 'all'>;
}

/** 发票统计结果。 */
export interface FinanceInvoiceStatsDto {
  currentMonthIssuedAmount: number;
  currentMonthIssuedCount: number;
  pendingCount: number;
  voidedCount: number;
}

/** 发票记录列表项。 */
export interface FinanceInvoiceRecordListItemDto {
  amount: number;
  applicantName: string;
  appliedAt: string;
  companyName: string;
  invoiceNo: string;
  invoiceType: string;
  invoiceTypeText: string;
  orderNo: string;
  recordId: string;
  status: string;
  statusText: string;
}

/** 发票记录分页结果。 */
export interface FinanceInvoiceRecordListResultDto {
  items: FinanceInvoiceRecordListItemDto[];
  page: number;
  pageSize: number;
  stats: FinanceInvoiceStatsDto;
  totalCount: number;
}

/** 发票记录详情。 */
export interface FinanceInvoiceRecordDetailDto {
  amount: number;
  applicantName: string;
  appliedAt: string;
  applyRemark?: string;
  companyName: string;
  contactEmail?: string;
  contactPhone?: string;
  invoiceNo: string;
  invoiceType: string;
  invoiceTypeText: string;
  issueRemark?: string;
  issuedAt?: string;
  issuedByUserId?: string;
  orderNo: string;
  recordId: string;
  status: string;
  statusText: string;
  taxpayerNumber?: string;
  voidReason?: string;
  voidedAt?: string;
  voidedByUserId?: string;
}

/** 发票开票请求。 */
export interface FinanceInvoiceIssuePayload {
  contactEmail?: string;
  issueRemark?: string;
  recordId: string;
}

/** 发票开票结果。 */
export interface FinanceInvoiceIssueResultDto {
  amount: number;
  companyName: string;
  contactEmail?: string;
  invoiceNo: string;
  issuedAt: string;
  recordId: string;
  status: string;
  statusText: string;
}

/** 发票作废请求。 */
export interface FinanceInvoiceVoidPayload {
  recordId: string;
  voidReason: string;
}

/** 发票申请请求。 */
export interface FinanceInvoiceApplyPayload {
  amount: number;
  applicantName: string;
  appliedAt?: string;
  applyRemark?: string;
  companyName: string;
  contactEmail?: string;
  contactPhone?: string;
  invoiceType: Exclude<FinanceInvoiceTypeFilter, 'all'>;
  orderNo: string;
  taxpayerNumber?: string;
}

/** 查询发票设置。 */
export async function getFinanceInvoiceSettingDetailApi() {
  return requestClient.get<FinanceInvoiceSettingDto>(
    '/finance/invoice/settings/detail',
  );
}

/** 保存发票设置。 */
export async function saveFinanceInvoiceSettingApi(
  payload: SaveFinanceInvoiceSettingPayload,
) {
  return requestClient.post<FinanceInvoiceSettingDto>(
    '/finance/invoice/settings/save',
    payload,
  );
}

/** 查询发票记录列表。 */
export async function getFinanceInvoiceRecordListApi(
  params: FinanceInvoiceRecordListQuery,
) {
  return requestClient.get<FinanceInvoiceRecordListResultDto>(
    '/finance/invoice/record/list',
    {
      params,
    },
  );
}

/** 查询发票记录详情。 */
export async function getFinanceInvoiceRecordDetailApi(params: {
  recordId: string;
}) {
  return requestClient.get<FinanceInvoiceRecordDetailDto>(
    '/finance/invoice/record/detail',
    {
      params,
    },
  );
}

/** 执行发票开票。 */
export async function issueFinanceInvoiceRecordApi(
  payload: FinanceInvoiceIssuePayload,
) {
  return requestClient.post<FinanceInvoiceIssueResultDto>(
    '/finance/invoice/record/issue',
    payload,
  );
}

/** 执行发票作废。 */
export async function voidFinanceInvoiceRecordApi(
  payload: FinanceInvoiceVoidPayload,
) {
  return requestClient.post<FinanceInvoiceRecordDetailDto>(
    '/finance/invoice/record/void',
    payload,
  );
}

/** 发起发票申请。 */
export async function applyFinanceInvoiceRecordApi(
  payload: FinanceInvoiceApplyPayload,
) {
  return requestClient.post<FinanceInvoiceRecordDetailDto>(
    '/finance/invoice/record/apply',
    payload,
  );
}
