import type {
  InterfaceActionResponse,
  InterfaceChangeLogItem,
  InterfaceDocInfo,
  InterfaceExportResponse,
  InterfaceFormValues,
  InterfaceGatewayFormValues,
  InterfaceGatewayPolicy,
  InterfaceIntegrationInfo,
  InterfaceItem,
  InterfaceListParams,
  InterfaceMockInfo,
  InterfaceRoutePreview,
} from '@/pages/InterfaceManagement/data';
import { request } from '@umijs/max';

export async function queryInterfaceList(params?: InterfaceListParams) {
  return request<{ data: InterfaceItem[]; total: number; success: boolean }>(
    '/api/interfaces',
    {
      method: 'GET',
      params,
    },
  );
}

export async function createInterface(data: InterfaceFormValues) {
  return request<InterfaceItem>('/api/interfaces', {
    method: 'POST',
    data,
  });
}

export async function updateInterface(id: string, data: InterfaceFormValues) {
  return request<InterfaceItem>(`/api/interfaces/${id}`, {
    method: 'PUT',
    data,
  });
}

export async function removeInterface(id: string) {
  return request<{ success: boolean }>(`/api/interfaces/${id}`, {
    method: 'DELETE',
  });
}

export async function queryInterfaceDocInfo(id: string) {
  return request<InterfaceDocInfo>(`/api/interfaces/${id}/doc`, {
    method: 'GET',
  });
}

export async function queryInterfaceGatewayPolicy(id: string) {
  return request<InterfaceGatewayPolicy>(`/api/interfaces/${id}/gateway`, {
    method: 'GET',
  });
}

export async function updateInterfaceGatewayPolicy(
  id: string,
  data: InterfaceGatewayFormValues,
) {
  return request<InterfaceGatewayPolicy>(`/api/interfaces/${id}/gateway`, {
    method: 'PUT',
    data,
  });
}

export async function previewInterfaceRoute(id: string) {
  return request<InterfaceRoutePreview>(
    `/api/interfaces/${id}/gateway/preview`,
    {
      method: 'GET',
    },
  );
}

export async function queryInterfaceMockInfo(id: string) {
  return request<InterfaceMockInfo>(`/api/interfaces/${id}/mock`, {
    method: 'GET',
  });
}

export async function startInterfaceDebug(id: string) {
  return request<InterfaceActionResponse>(`/api/interfaces/${id}/debug`, {
    method: 'POST',
  });
}

export async function queryInterfaceIntegrationInfo(id: string) {
  return request<InterfaceIntegrationInfo>(
    `/api/interfaces/${id}/integrations`,
    {
      method: 'GET',
    },
  );
}

export async function createInterfaceIntegration(
  id: string,
  data: Pick<InterfaceIntegrationInfo, 'partner' | 'channel'>,
) {
  return request<InterfaceIntegrationInfo>(
    `/api/interfaces/${id}/integrations`,
    {
      method: 'POST',
      data,
    },
  );
}

export async function manageInterfaceIntegrationKey(id: string) {
  return request<InterfaceIntegrationInfo>(
    `/api/interfaces/${id}/integrations/key`,
    {
      method: 'POST',
    },
  );
}

export async function queryInterfaceChangeLogs(id: string) {
  return request<InterfaceChangeLogItem[]>(`/api/interfaces/${id}/changelog`, {
    method: 'GET',
  });
}

export async function exportInterfaceChangeLog(id: string) {
  return request<InterfaceExportResponse>(
    `/api/interfaces/${id}/changelog/export`,
    {
      method: 'GET',
    },
  );
}
