// 商户店铺管理服务

interface Shop {
  id: string;
  merchantId: string;
  shopName: string;
  platform: string;
  shopUrl: string;
  status: 'ACTIVE' | 'INACTIVE' | 'SUSPENDED';
  createdAt: string;
  updatedAt: string;
}

interface ShopResponse {
  data: Shop[];
  total: number;
  page: number;
  pageSize: number;
}

// 模拟数据
const mockShops: Shop[] = [
  {
    id: '1',
    merchantId: '1',
    shopName: '商户A的Amazon店铺',
    platform: 'Amazon',
    shopUrl: 'https://www.amazon.com/shop/merchantA',
    status: 'ACTIVE',
    createdAt: '2026-03-01T10:00:00Z',
    updatedAt: '2026-03-01T10:00:00Z',
  },
  {
    id: '2',
    merchantId: '1',
    shopName: '商户A的eBay店铺',
    platform: 'eBay',
    shopUrl: 'https://www.ebay.com/str/merchantA',
    status: 'ACTIVE',
    createdAt: '2026-03-02T10:00:00Z',
    updatedAt: '2026-03-02T10:00:00Z',
  },
  {
    id: '3',
    merchantId: '2',
    shopName: '商户B的Shopee店铺',
    platform: 'Shopee',
    shopUrl: 'https://shopee.com.my/merchantB',
    status: 'INACTIVE',
    createdAt: '2026-03-03T10:00:00Z',
    updatedAt: '2026-03-03T10:00:00Z',
  },
  {
    id: '4',
    merchantId: '3',
    shopName: '商户C的TikTok店铺',
    platform: 'TikTok',
    shopUrl: 'https://www.tiktok.com/@merchantC',
    status: 'SUSPENDED',
    createdAt: '2026-03-04T10:00:00Z',
    updatedAt: '2026-03-04T10:00:00Z',
  },
];

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

// 创建商户店铺
export const createMerchantShop = async (shopData: Partial<Shop>): Promise<Shop> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const newShop: Shop = {
    id: `new-${Date.now()}`,
    merchantId: shopData.merchantId || '',
    shopName: shopData.shopName || '',
    platform: shopData.platform || '',
    shopUrl: shopData.shopUrl || '',
    status: shopData.status || 'INACTIVE',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString(),
  };
  mockShops.push(newShop);
  return newShop;
};

// 更新商户店铺
export const updateMerchantShop = async (shopId: string, shopData: Partial<Shop>): Promise<Shop> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const index = mockShops.findIndex(s => s.id === shopId);
  if (index === -1) {
    throw new Error('店铺不存在');
  }
  mockShops[index] = {
    ...mockShops[index],
    ...shopData,
    updatedAt: new Date().toISOString(),
  };
  return mockShops[index];
};

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

// 获取店铺详情
export const getShopById = async (shopId: string): Promise<Shop> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const shop = mockShops.find(s => s.id === shopId);
  if (!shop) {
    throw new Error('店铺不存在');
  }
  return shop;
};

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