// 商户结算管理服务

interface Settlement {
  id: string;
  merchantId: string;
  merchantName: string;
  periodStart: string;
  periodEnd: string;
  totalAmount: number;
  status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED';
  createdAt: string;
  updatedAt: string;
}

interface SettlementResponse {
  data: Settlement[];
  total: number;
  page: number;
  pageSize: number;
}

// 模拟数据
const mockSettlements: Settlement[] = [
  {
    id: '1',
    merchantId: '1',
    merchantName: '商户A',
    periodStart: '2026-02-01',
    periodEnd: '2026-02-29',
    totalAmount: 15000.00,
    status: 'COMPLETED',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-02T10:00:00Z',
  },
  {
    id: '2',
    merchantId: '2',
    merchantName: '商户B',
    periodStart: '2026-02-01',
    periodEnd: '2026-02-29',
    totalAmount: 8000.00,
    status: 'COMPLETED',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-02T10:00:00Z',
  },
  {
    id: '3',
    merchantId: '3',
    merchantName: '商户C',
    periodStart: '2026-02-01',
    periodEnd: '2026-02-29',
    totalAmount: 12000.00,
    status: 'PROCESSING',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-01T10:00:00Z',
  },
  {
    id: '4',
    merchantId: '1',
    merchantName: '商户A',
    periodStart: '2026-03-01',
    periodEnd: '2026-03-31',
    totalAmount: 0.00,
    status: 'PENDING',
    createdAt: '2026-04-01T10:00:00Z',
    updatedAt: '2026-04-01T10:00:00Z',
  },
];

// 获取商户结算列表
export const getMerchantSettlements = async (): Promise<SettlementResponse> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  return {
    data: mockSettlements,
    total: mockSettlements.length,
    page: 1,
    pageSize: 10,
  };
};

// 根据商户ID获取结算列表
export const getSettlementsByMerchantId = async (merchantId: string): Promise<Settlement[]> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  return mockSettlements.filter(s => s.merchantId === merchantId);
};

// 处理结算
export const processSettlement = async (settlementId: string): Promise<Settlement> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 1000));
  const index = mockSettlements.findIndex(s => s.id === settlementId);
  if (index === -1) {
    throw new Error('结算单不存在');
  }
  mockSettlements[index] = {
    ...mockSettlements[index],
    status: 'COMPLETED',
    updatedAt: new Date().toISOString(),
  };
  return mockSettlements[index];
};

// 获取结算详情
export const getSettlementById = async (settlementId: string): Promise<Settlement> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const settlement = mockSettlements.find(s => s.id === settlementId);
  if (!settlement) {
    throw new Error('结算单不存在');
  }
  return settlement;
};

// 创建结算单
export const createSettlement = async (settlementData: Partial<Settlement>): Promise<Settlement> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const newSettlement: Settlement = {
    id: `new-${Date.now()}`,
    merchantId: settlementData.merchantId || '',
    merchantName: settlementData.merchantName || '',
    periodStart: settlementData.periodStart || '',
    periodEnd: settlementData.periodEnd || '',
    totalAmount: settlementData.totalAmount || 0,
    status: 'PENDING',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };
  mockSettlements.push(newSettlement);
  return newSettlement;
};