import { GraphQLClient } from 'graphql-request';
import {
  CreateFeedbackMutationVariables,
  UpdateFeedbackMutationVariables,
  DeleteFeedbackMutationVariables,
  FeedbackWhereUniqueInput,
  CreateManyFeedbacksMutationVariables,
} from '../gql/graphql';
import { graphql } from '../gql';
import { BaseFunctions } from './BaseFunctions';
import { ListFeedbackQueryVariables } from '../gql/graphql';

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

  async listFeedback(options: ListFeedbackQueryVariables) {
    const query = graphql(`
      query ListFeedback(
        $where: FeedbackWhereInput
        $cursor: FeedbackWhereUniqueInput
        $orderBy: [FeedbackOrderByWithRelationInput!]
        $take: Int
        $skip: Int
        $distinct: [FeedbackScalarFieldEnum!]
      ) {
        feedbacks(
          where: $where
          cursor: $cursor
          orderBy: $orderBy
          take: $take
          skip: $skip
          distinct: $distinct
        ) {
          content
          createdAt
          id
          labelId
          updatedAt
          upvote
          userId
        }
      }
    `);

    return this.client.request(query, options);
  }

  async getFeedback(where: FeedbackWhereUniqueInput) {
    const query = graphql(`
      query GetFeedback($where: FeedbackWhereUniqueInput!) {
        feedback(where: $where) {
          id
          userId
          upvote
          content
          labelId
        }
      }
    `);

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

  async createFeedback(feedbackData: CreateFeedbackMutationVariables['data']) {
    const mutation = graphql(`
      mutation CreateFeedback($data: FeedbackCreateInput!) {
        createOneFeedback(data: $data) {
          id
          userId
          upvote
          content
          labelId
        }
      }
    `);

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

  async createManyFeedbacks(feedbackVariables: CreateManyFeedbacksMutationVariables['data']) {
    const mutation = graphql(`
      mutation CreateManyFeedbacks($data: [FeedbackCreateManyInput!]!) {
        createManyFeedback(data: $data) {
          count
        }
      }
    `);

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

  async updateFeedback(feedbackId: number, feedbackData: UpdateFeedbackMutationVariables['data']) {
    const mutation = graphql(`
      mutation UpdateFeedback($where: FeedbackWhereUniqueInput!, $data: FeedbackUpdateInput!) {
        updateOneFeedback(where: $where, data: $data) {
          id
          userId
          upvote
          content
          labelId
        }
      }
    `);

    const variables: UpdateFeedbackMutationVariables = {
      where: { id: feedbackId },
      data: feedbackData,
    };
    return this.request(mutation, variables);
  }

  async deleteFeedback(feedbackId: number) {
    const mutation = graphql(`
      mutation DeleteFeedback($where: FeedbackWhereUniqueInput!) {
        deleteOneFeedback(where: $where) {
          id
        }
      }
    `);

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

export { FeedbackFunctions };
