import { GraphQLClient } from 'graphql-request';
import {
  GetLabelQueryVariables,
  CreateLabelMutationVariables,
  UpdateLabelMutationVariables,
  DeleteLabelMutationVariables,
  LabelWhereUniqueInput,
} from '../gql/graphql';
import { graphql } from '../gql';
import { BaseFunctions } from './BaseFunctions';
import { ListLabelsQueryVariables } from '../gql/graphql';

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

  async listLabels(options: ListLabelsQueryVariables) {
    const query = graphql(`
      query ListLabels(
        $where: LabelWhereInput
        $skip: Int
        $take: Int
        $cursor: LabelWhereUniqueInput
        $distinct: [LabelScalarFieldEnum!]
        $orderBy: [LabelOrderByWithRelationInput!]
      ) {
        labels(
          where: $where
          skip: $skip
          take: $take
          orderBy: $orderBy
          cursor: $cursor
          distinct: $distinct
        ) {
          id
          value
          confidence
          predictionId
          createdAt
          name
          trainingPredictionId
          updatedAt
        }
      }
    `);
    return this.request(query, options);
  }

  async getLabel(where: LabelWhereUniqueInput) {
    const query = graphql(`
      query GetLabel($where: LabelWhereUniqueInput!) {
        label(where: $where) {
          id
          value
          confidence
          predictionId
        }
      }
    `);

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

  async createLabel(labelData: CreateLabelMutationVariables['data']) {
    const mutation = graphql(`
      mutation CreateLabel($data: LabelCreateInput!) {
        createOneLabel(data: $data) {
          id
          value
          confidence
          predictionId
        }
      }
    `);

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

  async updateLabel(labelId: number, labelData: UpdateLabelMutationVariables['data']) {
    const mutation = graphql(`
      mutation UpdateLabel($where: LabelWhereUniqueInput!, $data: LabelUpdateInput!) {
        updateOneLabel(where: $where, data: $data) {
          id
          value
          confidence
          predictionId
        }
      }
    `);

    const variables: UpdateLabelMutationVariables = {
      where: { id: labelId },
      data: labelData,
    };
    return this.request(mutation, variables);
  }

  async deleteLabel(labelId: number) {
    const mutation = graphql(`
      mutation DeleteLabel($where: LabelWhereUniqueInput!) {
        deleteOneLabel(where: $where) {
          id
        }
      }
    `);

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

export { LabelFunctions };
