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

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

  private constructor() {}

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

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

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

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

// 导出单例实例
const disabilityClientManager = DisabilityClientManager.getInstance();

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

// 导出类型定义
export type CreateDisabledPersonRequest = InferRequestType<typeof disabilityClient.createDisabledPerson.$post>['json'];
export type CreateDisabledPersonResponse = InferResponseType<typeof disabilityClient.createDisabledPerson.$post, 200>;

export type UpdateDisabledPersonRequest = InferRequestType<typeof disabilityClient.updateDisabledPerson.$post>['json'];
export type UpdateDisabledPersonResponse = InferResponseType<typeof disabilityClient.updateDisabledPerson.$post, 200>;

export type DeleteDisabledPersonRequest = InferRequestType<typeof disabilityClient.deleteDisabledPerson.$post>['json'];
export type DeleteDisabledPersonResponse = InferResponseType<typeof disabilityClient.deleteDisabledPerson.$post, 200>;

export type GetAllDisabledPersonsRequest = InferRequestType<typeof disabilityClient.getAllDisabledPersons.$get>['query'];
export type GetAllDisabledPersonsResponse = InferResponseType<typeof disabilityClient.getAllDisabledPersons.$get, 200>;

export type SearchDisabledPersonsRequest = InferRequestType<typeof disabilityClient.searchDisabledPersons.$get>['query'];
export type SearchDisabledPersonsResponse = InferResponseType<typeof disabilityClient.searchDisabledPersons.$get, 200>;

export type GetDisabledPersonRequest = InferRequestType<typeof disabilityClient.getDisabledPerson[':id']['$get']>['param'];
export type GetDisabledPersonResponse = InferResponseType<typeof disabilityClient.getDisabledPerson[':id']['$get'], 200>;

export type FindByIdCardRequest = InferRequestType<typeof disabilityClient.findByIdCard[':idCard']['$get']>['param'];
export type FindByIdCardResponse = InferResponseType<typeof disabilityClient.findByIdCard[':idCard']['$get'], 200>;

export type BatchCreateDisabledPersonsRequest = InferRequestType<typeof disabilityClient.batchCreateDisabledPersons.$post>['json'];
export type BatchCreateDisabledPersonsResponse = InferResponseType<typeof disabilityClient.batchCreateDisabledPersons.$post, 200>;

// 聚合API类型定义
export type CreateAggregatedDisabledPersonRequest = InferRequestType<typeof disabilityClient.createAggregatedDisabledPerson.$post>['json'];
export type CreateAggregatedDisabledPersonResponse = InferResponseType<typeof disabilityClient.createAggregatedDisabledPerson.$post, 200>;

export type GetAggregatedDisabledPersonRequest = InferRequestType<typeof disabilityClient.getAggregatedDisabledPerson[':id']['$get']>['param'];
export type GetAggregatedDisabledPersonResponse = InferResponseType<typeof disabilityClient.getAggregatedDisabledPerson[':id']['$get'], 200>;

export type UpdateAggregatedDisabledPersonRequest = InferRequestType<typeof disabilityClient.updateAggregatedDisabledPerson[':id']['$put']>['json'];
export type UpdateAggregatedDisabledPersonResponse = InferResponseType<typeof disabilityClient.updateAggregatedDisabledPerson[':id']['$put'], 200>;

export {
  disabilityClientManager
}