import { http } from './http';

export interface StoreBindingData {
  merchantId: string;
  platform: string;
  platformShopId: string;
  name: string;
  description?: string;
  authInfo: Record<string, any>;
}

export interface Store {
  id: string;
  merchantId: string;
  name: string;
  platform: string;
  platformShopId: string;
  description: string;
  status: string;
  created_at: string;
  updated_at: string;
}

export interface SyncResult {
  success: boolean;
  count: number;
}

export class OperationAgentService {
  /**
   * 绑定店铺
   */
  async bindStore(data: StoreBindingData): Promise<Store> {
    const response = await http.post<Store>('/api/operation-agent/stores', data);
    return response.data;
  }

  /**
   * 获取商户的店铺列表
   */
  async getStores(merchantId: string): Promise<Store[]> {
    const response = await http.get<Store[]>(`/api/operation-agent/stores/${merchantId}`);
    return response.data;
  }

  /**
   * 获取店铺详情
   */
  async getStore(storeId: string): Promise<Store> {
    const response = await http.get<Store>(`/api/operation-agent/stores/detail/${storeId}`);
    return response.data;
  }

  /**
   * 同步店铺商品
   */
  async syncProducts(storeId: string): Promise<SyncResult> {
    const response = await http.post<SyncResult>(`/api/operation-agent/stores/${storeId}/products/sync`);
    return response.data;
  }

  /**
   * 同步店铺订单
   */
  async syncOrders(storeId: string): Promise<SyncResult> {
    const response = await http.post<SyncResult>(`/api/operation-agent/stores/${storeId}/orders/sync`);
    return response.data;
  }

  /**
   * 更新商品价格
   */
  async updateProductPrice(storeId: string, productId: string, price: number): Promise<boolean> {
    const response = await http.put<boolean>(`/api/operation-agent/stores/${storeId}/products/${productId}/price`, { price });
    return response.data;
  }

  /**
   * 停用店铺
   */
  async deactivateStore(storeId: string): Promise<Store> {
    const response = await http.put<Store>(`/api/operation-agent/stores/${storeId}/deactivate`);
    return response.data;
  }

  /**
   * 重新激活店铺
   */
  async reactivateStore(storeId: string): Promise<Store> {
    const response = await http.put<Store>(`/api/operation-agent/stores/${storeId}/reactivate`);
    return response.data;
  }
}

export const operationAgentService = new OperationAgentService();
