/**
 * AI-generated course plan — types mirror the backend controller's
 * response shape in `encoach_ai_course.controllers.course_plan`.
 */

export type CoursePlanSkill =
  | "reading"
  | "writing"
  | "listening"
  | "speaking"
  | "grammar"
  | "vocabulary"
  | "integrated";

export type CoursePlanMaterialType =
  | "reading_text"
  | "listening_script"
  | "speaking_prompt"
  | "writing_prompt"
  | "grammar_lesson"
  | "vocabulary_list"
  | "practice"
  | "other";

export interface CoursePlanOutcome {
  code: string;
  description: string;
}

export interface CoursePlanGrammarItem {
  code: string;
  label: string;
  sub_items?: string[];
}

export interface CoursePlanAssessmentComponent {
  name: string;
  weight: number;
}

export interface CoursePlanAssessment {
  continuous_assessment?: {
    total_weight?: number;
    components?: CoursePlanAssessmentComponent[];
  };
  final_exam?: {
    total_weight?: number;
  };
}

export interface CoursePlanResource {
  type: string;
  citation: string;
}

export interface CoursePlanWeekItem {
  skill: CoursePlanSkill | string;
  outcome_codes: string[];
  remarks?: string;
}

export interface CoursePlanWeek {
  id: number;
  week_number: number;
  date_label: string;
  unit: string;
  focus: string;
  items: CoursePlanWeekItem[];
  material_count: number;
}

export interface CoursePlanMaterial {
  id: number;
  plan_id: number;
  week_id: number | null;
  week_number: number;
  skill: string;
  material_type: CoursePlanMaterialType | string;
  title: string;
  summary: string;
  /** Loose shape: depends on material_type. */
  body: Record<string, unknown>;
  body_text: string;
  media?: CoursePlanMedia[];
}

// ---------------------------------------------------------------------------
// Phase A — Reference sources
// ---------------------------------------------------------------------------

export type CoursePlanSourceKind = "file" | "url" | "text";
export type CoursePlanSourceStatus = "pending" | "indexing" | "indexed" | "failed";

export interface CoursePlanSource {
  id: number;
  plan_id: number;
  name: string;
  kind: CoursePlanSourceKind;
  file_name: string;
  mime_type: string;
  url: string;
  has_inline_text: boolean;
  status: CoursePlanSourceStatus;
  error: string;
  chunks_count: number;
  extracted_chars: number;
  indexed_at: string | null;
  created_at: string | null;
}

// ---------------------------------------------------------------------------
// Phase B — Deliverables preview / progress
// ---------------------------------------------------------------------------

export type DeliverableStatus = "planned" | "generated" | "ready";

export interface CoursePlanDeliverable {
  skill: string;
  material_type: string;
  material_id: number | null;
  title: string;
  status: DeliverableStatus;
  media: Array<{
    id: number;
    kind: "audio" | "image" | "video";
    status: string;
    provider: string;
  }>;
}

export interface CoursePlanDeliverablesWeek {
  week_number: number;
  date_label: string;
  unit: string;
  focus: string;
  items_total: number;
  deliverables: CoursePlanDeliverable[];
}

export interface CoursePlanDeliverables {
  summary: {
    total: number;
    planned: number;
    generated: number;
    ready: number;
    percent_ready: number;
    media: {
      audio: number;
      image: number;
      video: number;
      audio_ready: number;
      image_ready: number;
      video_ready: number;
    };
  };
  weeks: CoursePlanDeliverablesWeek[];
}

// ---------------------------------------------------------------------------
// Phase C — Multimedia
// ---------------------------------------------------------------------------

export type CoursePlanMediaKind = "audio" | "image" | "video";
export type CoursePlanMediaStatus = "queued" | "generating" | "ready" | "failed";
export type CoursePlanMediaProvider =
  | "polly"
  | "elevenlabs"
  | "openai_image"
  | "ffmpeg"
  | "elai"
  | "manual";

export interface CoursePlanMedia {
  id: number;
  plan_id: number;
  week_id: number | null;
  material_id: number | null;
  kind: CoursePlanMediaKind;
  provider: CoursePlanMediaProvider | string;
  title: string;
  voice: string;
  language: string;
  style: string;
  mime_type: string;
  size_bytes: number;
  duration_seconds: number;
  width: number;
  height: number;
  attachment_id: number | null;
  download_url: string;
  status: CoursePlanMediaStatus;
  error: string;
  cost_cents: number;
  created_at: string | null;
}

// ---------------------------------------------------------------------------
// Phase D — Assignments
// ---------------------------------------------------------------------------

export type CoursePlanAssignmentMode = "batch" | "students";
export type CoursePlanAssignmentState = "active" | "archived";

export interface CoursePlanAssignment {
  id: number;
  plan_id: number;
  plan_name: string;
  mode: CoursePlanAssignmentMode;
  batch_id: number | null;
  batch_name: string;
  student_user_ids: number[];
  student_user_names: string[];
  student_count: number;
  assigned_by_id: number | null;
  assigned_by_name: string;
  due_date: string | null;
  message: string;
  state: CoursePlanAssignmentState;
  created_at: string | null;
}

export interface CoursePlan {
  id: number;
  name: string;
  course_id: number | null;
  course_name: string;
  cefr_level: string;
  total_weeks: number;
  contact_hours_per_week: number;
  skills_division: string;
  description: string;
  status: "draft" | "generated" | "approved" | "archived";
  objectives: string[];
  outcomes: Partial<Record<CoursePlanSkill, CoursePlanOutcome[]>>;
  grammar: CoursePlanGrammarItem[];
  assessment: CoursePlanAssessment;
  resources: CoursePlanResource[];
  week_count: number;
  material_count: number;
  source_count?: number;
  media_count?: number;
  assignment_count?: number;
  created_at: string | null;
  weeks?: CoursePlanWeek[];
  materials?: CoursePlanMaterial[];
  sources?: CoursePlanSource[];
  media?: CoursePlanMedia[];
  assignments?: CoursePlanAssignment[];
  /** Present on the student-list endpoint payload. */
  assignment?: CoursePlanAssignment;
}

export interface CoursePlanGenerateBrief {
  title: string;
  cefr_level?: string;
  total_weeks?: number;
  contact_hours_per_week?: number;
  skills_division?: string;
  grammar_focus?: string[];
  resources?: string[];
  learner_profile?: string;
  notes?: string;
  course_id?: number;
}
