// 商户管理服务

interface Merchant {
  id: string;
  tenantId: string;
  companyName: string;
  businessLicense: string;
  contactEmail: string;
  contactPhone: string;
  status: 'PENDING' | 'ACTIVE' | 'SUSPENDED' | 'TERMINATED';
  tier: 'BASIC' | 'PRO' | 'ENTERPRISE';
  traceId: string;
  createdAt: string;
  updatedAt: string;
}

interface MerchantResponse {
  data: Merchant[];
  total: number;
  page: number;
  pageSize: number;
}

// 模拟数据
const mockMerchants: Merchant[] = [
  {
    id: '1',
    tenantId: 'tenant1',
    companyName: '商户A',
    businessLicense: '1234567890',
    contactEmail: 'merchantA@example.com',
    contactPhone: '13800138001',
    status: 'ACTIVE',
    tier: 'PRO',
    traceId: 'trace1',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-01T10:00:00Z',
  },
  {
    id: '2',
    tenantId: 'tenant2',
    companyName: '商户B',
    businessLicense: '0987654321',
    contactEmail: 'merchantB@example.com',
    contactPhone: '13900139001',
    status: 'PENDING',
    tier: 'BASIC',
    traceId: 'trace2',
    createdAt: '2026-03-02T10:00:00Z',
    updatedAt: '2026-03-02T10:00:00Z',
  },
  {
    id: '3',
    tenantId: 'tenant3',
    companyName: '商户C',
    businessLicense: '1122334455',
    contactEmail: 'merchantC@example.com',
    contactPhone: '13700137001',
    status: 'SUSPENDED',
    tier: 'ENTERPRISE',
    traceId: 'trace3',
    createdAt: '2026-03-03T10:00:00Z',
    updatedAt: '2026-03-03T10:00:00Z',
  },
];

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

// 创建商户
export const createMerchant = async (merchantData: Partial<Merchant>): Promise<Merchant> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const newMerchant: Merchant = {
    id: `new-${Date.now()}`,
    tenantId: merchantData.tenantId || 'default-tenant',
    companyName: merchantData.companyName || '',
    businessLicense: merchantData.businessLicense || '',
    contactEmail: merchantData.contactEmail || '',
    contactPhone: merchantData.contactPhone || '',
    status: merchantData.status || 'PENDING',
    tier: merchantData.tier || 'BASIC',
    traceId: `trace-${Date.now()}`,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };
  mockMerchants.push(newMerchant);
  return newMerchant;
};

// 更新商户
export const updateMerchant = async (merchantId: string, merchantData: Partial<Merchant>): Promise<Merchant> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const index = mockMerchants.findIndex(m => m.id === merchantId);
  if (index === -1) {
    throw new Error('商户不存在');
  }
  mockMerchants[index] = {
    ...mockMerchants[index],
    ...merchantData,
    updatedAt: new Date().toISOString(),
  };
  return mockMerchants[index];
};

// 删除商户
export const deleteMerchant = async (merchantId: string): Promise<boolean> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const index = mockMerchants.findIndex(m => m.id === merchantId);
  if (index === -1) {
    throw new Error('商户不存在');
  }
  mockMerchants.splice(index, 1);
  return true;
};

// 获取商户详情
export const getMerchantById = async (merchantId: string): Promise<Merchant> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const merchant = mockMerchants.find(m => m.id === merchantId);
  if (!merchant) {
    throw new Error('商户不存在');
  }
  return merchant;
};