export interface Course {
  id: number;
  title: string;
  code: string;
  branch_id?: number | null;
  branch_name?: string;
  subject_id?: number;
  subject_name?: string;
  encoach_subject_id?: number | null;
  encoach_subject_name?: string;
  topic_ids?: number[];
  topic_names?: string[];
  learning_objective_ids?: number[];
  learning_objective_names?: string[];
  tag_ids?: number[];
  tags?: { id: number; name: string; color: string }[];
  difficulty_level?: "beginner" | "intermediate" | "advanced" | "";
  cefr_level?: string;
  instructor_id: number;
  instructor_name: string;
  description: string;
  level: "Beginner" | "Intermediate" | "Advanced";
  modules: CourseModule[];
  enrolled: number;
  max_capacity: number;
  status: "draft" | "active" | "archived";
  start_date: string;
  end_date: string;
  progress?: number;
  chapter_count?: number;
  resource_count?: number;
  objective_count?: number;
  section_count?: number;
  sections?: CourseSection[];
}

export interface CourseSection {
  id: number;
  name: string;
  code: string;
  sequence: number;
  active: boolean;
  notes?: string;
  course_id: number;
  course_name: string;
  entity_id?: number | null;
  entity_name?: string;
  branch_id?: number | null;
  branch_name?: string;
  batch_count?: number;
}

export interface CourseModule {
  id: number;
  title: string;
  sequence: number;
  lessons: CourseLesson[];
}

export interface CourseLesson {
  id: number;
  title: string;
  type: "video" | "reading" | "quiz" | "interactive";
  duration: string;
  completed?: boolean;
}

export interface Batch {
  id: number;
  name: string;
  branch_id?: number | null;
  branch_name?: string;
  classroom_id?: number | null;
  classroom_name?: string;
  course_section_id?: number | null;
  course_section_name?: string;
  course_section_code?: string;
  term_key?: string;
  course_id: number;
  course_name: string;
  /** @deprecated single teacher kept for back-compat. Use `teacher_ids`. */
  teacher_id: number;
  /** @deprecated single teacher kept for back-compat. */
  teacher_name: string;
  teacher_ids?: number[];
  teacher_names?: string[];
  student_ids: number[];
  student_count: number;
  start_date: string;
  end_date: string;
  schedule: string;
  status: "active" | "completed" | "upcoming";
  capacity: number;
}

export interface TimetableSession {
  id: number;
  course_id: string | number;
  course_name: string;
  teacher_name: string;
  batch_name: string;
  day: "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday";
  start_time: string;
  end_time: string;
  room: string;
  /** Optional hex/rgb used by the weekly grid renderer. */
  color?: string;
}

export interface AttendanceRecord {
  id: number;
  date: string;
  course_id: number;
  course_name: string;
  student_id: number;
  student_name: string;
  status: "present" | "absent" | "late" | "excused";
}

export interface GradeRecord {
  id: number;
  course_id: number;
  course_name: string;
  student_id: number;
  student_name: string;
  assignment_title: string;
  grade: number;
  max_grade: number;
  date: string;
  type: string;
}

export interface CourseCreateRequest {
  title: string;
  code: string;
  subject_id?: number;
  encoach_subject_id?: number;
  topic_ids?: number[];
  learning_objective_ids?: number[];
  tag_ids?: number[];
  difficulty_level?: "beginner" | "intermediate" | "advanced";
  cefr_level?: string;
  instructor_id: number;
  description: string;
  level: "Beginner" | "Intermediate" | "Advanced";
  max_capacity: number;
  start_date: string;
  end_date: string;
}

/** OpenEduCat `op.student` row from `/api/students` */
export interface LmsStudentRecord {
  id: number;
  name: string;
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  gender: string;
  enrollment_date: string | null;
  status: string;
  course_ids: number[];
  batch_id: number | null;
  batch_name: string;
  partner_id: number;
  user_id: number | null;
  branch_id?: number | null;
  branch_name?: string;
}

/** OpenEduCat `op.faculty` row from `/api/teachers` */
export interface LmsTeacherRecord {
  id: number;
  name: string;
  first_name: string;
  last_name: string;
  email: string;
  phone: string;
  gender: string;
  birth_date: string | null;
  partner_id: number;
  department_id: number | null;
  department_name: string;
  specialization: string;
  subject_names: string[];
  branch_id?: number | null;
  branch_name?: string;
}

export interface LmsBranch {
  id: number;
  name: string;
  code: string;
  active: boolean;
  notes: string;
  entity_id: number | null;
  entity_name: string;
  course_count: number;
  batch_count: number;
  student_count: number;
  teacher_count: number;
}


/** Enrolled course returned by GET /api/student/my-courses with progress data. */
export interface MyEnrolledCourse {
  id: number;
  name: string;
  title: string;
  code: string;
  description: string;
  status: string;
  student_count: number;
  max_capacity: number;
  batch_id: number | null;
  batch_name: string;
  chapter_count: number;
  total_materials: number;
  completed_chapters: number;
  progress: number;
}

export interface EnrollStudentRequest {
  course_id?: number;
  course_ids?: number[];
  batch_id?: number;
}

export interface BulkEnrollRequest {
  student_ids?: number[];
  batch_id?: number;
}

export interface LmsStudentCreateRequest {
  first_name: string;
  last_name: string;
  email: string;
  phone?: string;
  gender?: string;
  birth_date?: string;
  course_id?: number;
  batch_id?: number;
  academic_year_id?: number;
  /** If set, used for new portal `res.users` (SPA login). */
  password?: string;
  /** Default true: create portal user linked to `op.student`. */
  create_portal_user?: boolean;
  branch_id?: number;
}

export interface LmsTeacherCreateRequest {
  first_name: string;
  last_name: string;
  email: string;
  phone?: string;
  gender?: "male" | "female";
  birth_date?: string;
  department_id?: number;
  specialization?: string;
  branch_id?: number;
}
