import { get, post, put, del } from './apiClient';
import type {
  ServiceInfo,
  ServiceApi,
  CreateServiceRequest,
  UpdateServiceRequest,
  CreateServiceApiRequest,
  UpdateServiceApiRequest,
  ApiDetailInfo,
  SaveApiSpecRequest,
  SaveApiParamRequest,
  ApiSpecInfo,
  ApiParamInfo,
} from '../types/service';
import type { ApiDomainInfo } from '../types/apihub';

export interface CreateDomainRequest {
  domainName: string;
  iconPath?: string | null;
  sortOrder?: number;
}

export interface UpdateDomainRequest {
  domainName: string;
  iconPath?: string | null;
  sortOrder?: number;
}

export const getServices = () => get<ServiceInfo[]>('/services');
export const createService = (req: CreateServiceRequest) => post<ServiceInfo>('/services', req);
export const updateService = (id: number, req: UpdateServiceRequest) => put<ServiceInfo>(`/services/${id}`, req);
export const deleteService = (id: number) => del<void>(`/services/${id}`);
export const getServiceApis = (serviceId: number) => get<ServiceApi[]>(`/services/${serviceId}/apis`);
export const createServiceApi = (serviceId: number, req: CreateServiceApiRequest) =>
  post<ServiceApi>(`/services/${serviceId}/apis`, req);
export const updateServiceApi = (serviceId: number, apiId: number, req: UpdateServiceApiRequest) =>
  put<ServiceApi>(`/services/${serviceId}/apis/${apiId}`, req);
export const deleteServiceApi = (serviceId: number, apiId: number) =>
  del<void>(`/services/${serviceId}/apis/${apiId}`);
export const getApiDetail = (serviceId: number, apiId: number) =>
  get<ApiDetailInfo>(`/services/${serviceId}/apis/${apiId}/spec`);
export const saveApiSpec = (serviceId: number, apiId: number, req: SaveApiSpecRequest) =>
  put<ApiSpecInfo>(`/services/${serviceId}/apis/${apiId}/spec`, req);
export const saveApiParams = (serviceId: number, apiId: number, params: SaveApiParamRequest[]) =>
  put<ApiParamInfo[]>(`/services/${serviceId}/apis/${apiId}/params`, params);

export const getDomains = () => get<ApiDomainInfo[]>('/domains');
export const createDomain = (req: CreateDomainRequest) => post<ApiDomainInfo>('/domains', req);
export const updateDomain = (id: number, req: UpdateDomainRequest) => put<ApiDomainInfo>(`/domains/${id}`, req);
export const deleteDomain = (id: number) => del<void>(`/domains/${id}`);
