import { orderRoutes } from '@d8d/allin-order-module';
import { rpcClient } from '@d8d/shared-ui-components/utils/hc'
import type { InferRequestType, InferResponseType } from 'hono/client';

export class OrderClientManager {
  private static instance: OrderClientManager;
  private client: ReturnType<typeof rpcClient<typeof orderRoutes>> | null = null;

  private constructor() {}

  public static getInstance(): OrderClientManager {
    if (!OrderClientManager.instance) {
      OrderClientManager.instance = new OrderClientManager();
    }
    return OrderClientManager.instance;
  }

  // 初始化客户端
  public init(baseUrl: string = '/'): ReturnType<typeof rpcClient<typeof orderRoutes>> {
    return this.client = rpcClient<typeof orderRoutes>(baseUrl);
  }

  // 获取客户端实例
  public get(): ReturnType<typeof rpcClient<typeof orderRoutes>> {
    if (!this.client) {
      return this.init()
    }
    return this.client;
  }

  // 重置客户端（用于测试或重新初始化）
  public reset(): void {
    this.client = null;
  }
}

// 导出单例实例
const orderClientManager = OrderClientManager.getInstance();

// 导出默认客户端实例（延迟初始化）
export const orderClient = orderClientManager.get()

// 导出类型定义
export type CreateOrderRequest = InferRequestType<typeof orderClient.create.$post>['json'];
export type CreateOrderResponse = InferResponseType<typeof orderClient.create.$post, 200>;

export type UpdateOrderRequest = InferRequestType<typeof orderClient.update[':id']['$put']>['json'];
export type UpdateOrderResponse = InferResponseType<typeof orderClient.update[':id']['$put'], 200>;

export type DeleteOrderRequest = InferRequestType<typeof orderClient.delete[':id']['$delete']>['param'];
export type DeleteOrderResponse = InferResponseType<typeof orderClient.delete[':id']['$delete'], 200>;

export type GetAllOrdersRequest = InferRequestType<typeof orderClient.list.$get>['query'];
export type GetAllOrdersResponse = InferResponseType<typeof orderClient.list.$get, 200>;

export type GetOrderByIdRequest = InferRequestType<typeof orderClient.detail[':id']['$get']>['param'];
export type GetOrderByIdResponse = InferResponseType<typeof orderClient.detail[':id']['$get'], 200>;

export type ActivateOrderRequest = InferRequestType<typeof orderClient.activate[':orderId']['$post']>['param'];
export type ActivateOrderResponse = InferResponseType<typeof orderClient.activate[':orderId']['$post'], 200>;

export type CloseOrderRequest = InferRequestType<typeof orderClient.close[':orderId']['$post']>['param'];
export type CloseOrderResponse = InferResponseType<typeof orderClient.close[':orderId']['$post'], 200>;

export type BatchAddPersonsRequest = InferRequestType<typeof orderClient[':orderId']['persons']['batch']['$post']>['json'];
export type BatchAddPersonsResponse = InferResponseType<typeof orderClient[':orderId']['persons']['batch']['$post'], 200>;

export type CreateOrderPersonAssetRequest = InferRequestType<typeof orderClient.assets.create.$post>['json'];
export type CreateOrderPersonAssetResponse = InferResponseType<typeof orderClient.assets.create.$post, 200>;

export type QueryOrderPersonAssetRequest = InferRequestType<typeof orderClient.assets.query.$get>['query'];
export type QueryOrderPersonAssetResponse = InferResponseType<typeof orderClient.assets.query.$get, 200>;

export type DeleteOrderPersonAssetRequest = InferRequestType<typeof orderClient.assets.delete[':id']['$delete']>['param'];
export type DeleteOrderPersonAssetResponse = InferResponseType<typeof orderClient.assets.delete[':id']['$delete'], 200>;

export {
  orderClientManager
}