import { GraphQLClient } from 'graphql-request';
import { FeedbackFunctions } from "./FeedbackFunctions";
import { LabelFunctions } from "./LabelFunctions";
import { InferenceBackendFunctions } from "./InferenceBackendFunctions";
import { PredictionFunctions } from "./PredictionFunctions";
import { UserFunctions } from "./UserFunctions";
import { DatasetFunctions } from './DatasetFunctions';
import { AuthConfiguration, Configuration } from './types';

class ProtegeEngineSDK {
  private client: GraphQLClient;
  user: UserFunctions;
  inferenceBackend: InferenceBackendFunctions;
  prediction: PredictionFunctions; // Add the prediction property
  label: LabelFunctions; // Add the label property
  feedback: FeedbackFunctions; // Add the feedback property
  dataset: DatasetFunctions;

  constructor({ endpoint, auth }: Configuration) {
    if (!auth) {
      throw new Error('No API key provided');
    }
    if (!endpoint) {
      throw new Error('No endpoint provided');
    }
    this.client = new GraphQLClient(endpoint, {
      requestMiddleware: async (request) => {
        // console.log(`DEBUG -- request: ${JSON.stringify(request, null, 2)}`)
        const authHeader = await auth.getAuthHeader();
        const newRequest = {
          ...request,
          headers: {
            ...request.headers,
            Authorization: authHeader,
          },
        };
        // console.log(`DEBUG -- newRequest: ${JSON.stringify(newRequest, null, 2)}`)
        return newRequest;
      }
    });
    this.user = new UserFunctions(this.client);
    this.inferenceBackend = new InferenceBackendFunctions(this.client);
    this.prediction = new PredictionFunctions(this.client);
    this.label = new LabelFunctions(this.client);
    this.feedback = new FeedbackFunctions(this.client);
    this.dataset = new DatasetFunctions(this.client);
  }

  // ... Other shared functions
}

export { ProtegeEngineSDK };
