import { api } from "@/lib/api-client";
import type { ExamModule } from "@/types";

export interface GenerationParams {
  title?: string;
  label?: string;
  entity_id?: number;
  subject_id?: number;
  topic_id?: number;
  difficulty?: string;
  count?: number;
  topic?: string;
  passage_length?: string;
  task_type?: string;
  part?: string;
  question_count?: number;
}

export interface PersonaContext {
  exam_mode?: "official" | "practice" | string;
  exam_title?: string;
  exam_label?: string;
  structure_name?: string;
  passage_type?: string;
  task_type?: string;
  section_type?: string;
  part?: string;
  category?: string;
  subject_name?: string;
  subject_id?: number;
  entity_id?: number;
  entity_name?: string;
  rubric_name?: string;
  rubric_id?: number | string;
  grading_system?: string;
  access_type?: string;
  course_id?: number;
  module?: string;
}

export interface PassageGenerationParams extends PersonaContext {
  topic?: string;
  difficulty?: string;
  word_count?: number;
  type?: string;
}

export interface ExerciseConfig extends PersonaContext {
  passage_index: number;
  exercise_types: string[];
  count_per_type?: number;
}

export interface ModuleConfig {
  module: ExamModule;
  timer_minutes: number;
  difficulty: string[];
  access_type: string;
  entities?: number[];
  approval_workflow?: string;
  rubric_criteria_group?: string;
  rubric_criteria?: string;
  grading_system?: string;
  shuffling_enabled: boolean;
  passages?: PassageConfig[];
  sections?: SectionConfig[];
  tasks?: TaskConfig[];
  speaking_parts?: SpeakingPartConfig[];
}

export interface PassageConfig {
  index: number;
  category?: string;
  type?: string;
  divider?: string;
  text?: string;
  exercise_types: string[];
  exercises?: unknown[];
}

export interface SectionConfig {
  type: string;
  category?: string;
  divider?: string;
  audio_context?: string;
  audio_url?: string;
  exercise_types: string[];
  exercises?: unknown[];
}

export interface TaskConfig {
  index: number;
  category?: string;
  type?: string;
  divider?: string;
  instructions?: string;
  word_limit: number;
  marks: number;
  images?: string[];
}

export interface SpeakingPartConfig {
  type: string;
  category?: string;
  divider?: string;
  script?: string;
  video_url?: string;
  avatar_id?: string;
  marks: number;
  topics?: string[];
}

export const generationService = {
  generate(module: ExamModule, params: GenerationParams): Promise<{ questions: unknown[] }> {
    return api.post(`/exam/${module}/generate`, params);
  },

  saveGenerated(module: ExamModule, data: unknown): Promise<{ saved: number; module: string }> {
    const payload = Array.isArray(data) ? { questions: data } : data;
    return api.post(`/exam/${module}/generate/save`, payload);
  },

  generatePassage(params: PassageGenerationParams): Promise<{ passage: string; title?: string }> {
    return api.post("/exam/reading/generate", {
      ...params,
      generate_passage: true,
    });
  },

  generateExercises(
    module: ExamModule,
    config: ExerciseConfig & {
      passage_text?: string;
      type_counts?: Record<string, number>;
      type_instructions?: Record<string, string>;
      type_difficulties?: Record<string, string>;
      difficulty?: string;
    },
  ): Promise<{ questions: unknown[] }> {
    return api.post(`/exam/${module}/generate`, {
      ...config,
      generate_exercises: true,
    });
  },

  generateWritingInstructions(
    params: PersonaContext & { topic?: string; difficulty?: string; task_type?: string; word_limit?: number },
  ): Promise<{ instructions: string }> {
    return api.post("/exam/writing/generate", {
      ...params,
      generate_instructions: true,
    });
  },

  generateSpeakingScript(
    params: PersonaContext & { topics?: string[]; difficulty?: string; part?: string },
  ): Promise<{ script: string }> {
    return api.post("/exam/speaking/generate", {
      ...params,
      generate_script: true,
    });
  },

  generateListeningContext(
    params: PersonaContext & { topic?: string; section_type?: string; difficulty?: string },
  ): Promise<{ context: string }> {
    return api.post("/exam/listening/generate", {
      ...params,
      generate_context: true,
    });
  },

  submitExam(data: {
    title: string;
    label: string;
    exam_mode?: string;
    structure_id?: number;
    modules: Record<string, unknown>;
    skip_approval?: boolean;
  }): Promise<{ exam_id: number; status: string; template_id?: number; total_questions?: number }> {
    return api.post("/exam/generation/submit", data);
  },
};
