import { GraphQLClient } from 'graphql-request';
import {
  GetInferenceBackendQueryVariables,
  CreateInferenceBackendMutationVariables,
  UpdateInferenceBackendMutationVariables,
  DeleteInferenceBackendMutationVariables,
} from '../gql/graphql';
import { graphql } from '../gql';
import { BaseFunctions } from './BaseFunctions';
import { ListInferenceBackendsQueryVariables } from '../gql/graphql';

class InferenceBackendFunctions extends BaseFunctions {
  
  constructor(client: GraphQLClient) {
    super(client);
  }

  async listInferenceBackends(options: ListInferenceBackendsQueryVariables) {
    const query = graphql(`
      query ListInferenceBackends($where: InferenceBackendWhereInput, $skip: Int, $take: Int, $cursor: InferenceBackendWhereUniqueInput, $distinct: [InferenceBackendScalarFieldEnum!]) {
        inferenceBackends(where: $where, skip: $skip, take: $take, orderBy: { createdAt: desc }, cursor: $cursor, distinct: $distinct) {
          id
          name
          active
          urls
          apiKey
        }
      }
    `);

    return this.request(query, options);
  }

  async getInferenceBackend(modelId: number) {
    const query = graphql(`
      query GetInferenceBackend($where: InferenceBackendWhereUniqueInput!) {
        inferenceBackend(where: $where) {
          id
          name
          active
          urls
          apiKey
        }
      }
    `);

    return this.request(query, { where: { id: modelId } });
  }

  async createInferenceBackend(InferenceBackendData: CreateInferenceBackendMutationVariables['data']) {
    const mutation = graphql(`
      mutation CreateInferenceBackend($data: InferenceBackendCreateInput!) {
        createOneInferenceBackend(data: $data) {
          id
          name
          active
          urls
          apiKey
        }
      }
    `);

    return this.request(mutation, { data: InferenceBackendData });
  }

  async updateInferenceBackend(
    modelId: number,
    InferenceBackendData: UpdateInferenceBackendMutationVariables['data']
  ) {
    const mutation = graphql(`
      mutation UpdateInferenceBackend(
        $where: InferenceBackendWhereUniqueInput!
        $data: InferenceBackendUpdateInput!
      ) {
        updateOneInferenceBackend(where: $where, data: $data) {
          id
          name
          active
          urls
          apiKey
        }
      }
    `);

    const variables: UpdateInferenceBackendMutationVariables = {
      where: { id: modelId },
      data: InferenceBackendData,
    };
    return this.request(mutation, variables);
  }

  async deleteInferenceBackend(modelId: number) {
    const mutation = graphql(`
      mutation DeleteInferenceBackend($where: InferenceBackendWhereUniqueInput!) {
        deleteOneInferenceBackend(where: $where) {
          id
        }
      }
    `);

    const variables: DeleteInferenceBackendMutationVariables = { where: { id: modelId } };
    return this.request(mutation, variables);
  }
}

export { InferenceBackendFunctions };
