import { HttpClient, httpClient } from '../core/http-client';
import {environment} from "../../environments/environment";
import type {GeneratedJobApplication} from "../models/generated-job-application";
import type {PredefinedUserInputInterface} from "../models/predefined-user-input.interface";
import type {AiGeneratedCVDescription} from "../models/ai-generated-cv-description.interface";
import type {ApplicationExamination} from "../models/application-examination.interface";


export class AiHandlerService {
  constructor(private http: HttpClient = httpClient) {}

  generateApplication(isStar: boolean, jobId: string, language: string, userInput: string): Promise<{ai_generated_job_application_id: number}>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/create_my_application';
    let payload = {
      isStar: isStar,
      jobId: jobId,
      language: language,
      userInput: userInput
    };
    return this.http.post<{ai_generated_job_application_id: number}>(url, payload);
  }

  listGeneratedJobApplications(jobId: string): Promise<GeneratedJobApplication[]>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/list_created_job_application/' + jobId;
    return this.http.get<GeneratedJobApplication[]>(url);
  }

  listPredefinedUserInput(): Promise<PredefinedUserInputInterface[]>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/list_predefined_user_input';
    return this.http.get<PredefinedUserInputInterface[]>(url);
  }

  getJobApplication(aiGeneratedJobApplicationId: number): Promise<GeneratedJobApplication>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/' + aiGeneratedJobApplicationId;
    return this.http.get<GeneratedJobApplication>(url);
  }

  updateMyCvDescriptions(language: string): Promise<{ai_generated_updated_cv_description_id: number}>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/update_my_cv_descriptions';
    let payload= {
      language: language
    };
    return this.http.post<{ai_generated_updated_cv_description_id: number}>(url, payload);
  }

  updateStatesOnMyCvDescription(aiGeneratedUpdatedCvDescriptionId: number, state: number, subState: number): Promise<any>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/update_states_on_my_cv_descriptions/' + aiGeneratedUpdatedCvDescriptionId;
    let payload = {
      state: state,
      subState: subState
    }
    return this.http.put(url, payload);
  }

  getMyCvDescriptions(): Promise<AiGeneratedCVDescription>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/get_my_cv_descriptions';
    return this.http.get<AiGeneratedCVDescription>(url);
  }

  submitJobApplicationRating(applicationId: number, rating: number, comment: string): Promise<any>{
    let url = environment.backendApiV2 + '1.0.0/client/ai_handler/job_application_rating';
    let payload = {
      ai_generated_job_application_id: applicationId,
      rating: rating,
      rating_text: comment
    };
    return this.http.post(url, payload);
  }

  startApplicationExamination(aiGeneratedJobApplicationId: number): Promise<{ai_generated_job_application_examination_id: string}>{
    const url = environment.backendApiV2 + '1.0.0/client/ai_handler/create_application_examination';
    const payload = { ai_generated_job_application_id: aiGeneratedJobApplicationId };
    return this.http.post<{ai_generated_job_application_examination_id: string}>(url, payload);
  }

  getApplicationExamination(examinationId: string): Promise<ApplicationExamination>{
    const url = environment.backendApiV2 + '1.0.0/client/ai_handler/application_examination/' + encodeURIComponent(examinationId);
    return this.http.get<ApplicationExamination>(url);
  }

  getApplicationExaminationByJobApplicationId(aiGeneratedJobApplicationId: number): Promise<ApplicationExamination>{
    const url = environment.backendApiV2 + '1.0.0/client/ai_handler/application_examination_by_job_application/' + aiGeneratedJobApplicationId;
    return this.http.get<ApplicationExamination>(url);
  }
}
