/**
 * Timetable System Type Definitions
 * Comprehensive types for classes, timetables, lessons, and enrollment
 */

// ============================================================================
// Enums
// ============================================================================

export type EnrollmentStatus = 'pending' | 'approved' | 'rejected';
export type LessonStatus = 'scheduled' | 'completed' | 'cancelled';
export type TimetableType = 'adhoc' | 'recurring';
export type DayOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6; // 0 = Sunday

// ============================================================================
// Base Interfaces
// ============================================================================

export interface Class {
  id: string;
  name: string;
  subject: string;
  school_year: string;
  academic_term: string;
  description?: string;
  created_by: string;
  created_at: string;
  updated_at: string;
  is_active: boolean;
  code?: string;
  institute_id?: string;
}

export interface ClassWithRole extends Class {
  role: 'teacher' | 'student' | 'assistant';
  status: string;
  class_id?: string;
  is_primary_teacher?: boolean;
}

export interface ClassTeacher {
  id: string;
  class_id: string;
  teacher_id: string;
  is_primary: boolean;
  assigned_at: string;
}

export interface ClassStudent {
  id: string;
  class_id: string;
  student_id: string;
  enrolled_at: string;
}

export interface Timetable {
  id: string;
  class_id: string;
  name: string;
  type: TimetableType;
  start_date: string;
  end_date: string;
  recurrence_rule?: string;
  is_active: boolean;
  created_by: string;
  created_at: string;
  updated_at: string;
}

export interface TimetableTeacher {
  id: string;
  timetable_id: string;
  teacher_id: string;
  is_primary: boolean;
  assigned_at: string;
}

export interface TimetableLesson {
  id: string;
  timetable_id: string;
  day_of_week: DayOfWeek;
  start_time: string; // HH:MM format
  end_time: string;   // HH:MM format
  room?: string;
  max_students?: number;
  created_at: string;
}

export interface Lesson {
  id: string;
  timetable_id: string;
  lesson_definition_id: string;
  scheduled_date: string;
  start_time: string;
  end_time: string;
  room?: string;
  status: LessonStatus;
  cancellation_reason?: string;
  created_at: string;
  updated_at: string;
}

export interface LessonWhiteboard {
  id: string;
  lesson_id: string;
  whiteboard_id: string;
  student_id?: string;
  created_at: string;
}

export interface EnrollmentRequest {
  id: string;
  class_id: string;
  student_id: string;
  status: EnrollmentStatus;
  request_message?: string;
  response_message?: string;
  created_at: string;
  updated_at: string;
}

// ============================================================================
// Profile Interfaces (for joins)
// ============================================================================

export interface UserProfile {
  id: string;
  email: string;
  full_name?: string;
  avatar_url?: string;
  role: 'teacher' | 'student' | 'admin';
}

// ============================================================================
// Extended Interfaces (with relations)
// ============================================================================

export interface ClassWithRelations extends Class {
  teachers?: Array<ClassTeacher & { profile: UserProfile }>;
  students?: Array<ClassStudent & { profile: UserProfile }>;
  timetables?: Timetable[];
  enrollment_requests?: Array<EnrollmentRequestWithProfile>;
  student_count?: number;
}

export interface TimetableWithRelations extends Timetable {
  class?: Class;
  teachers?: Array<TimetableTeacher & { profile: UserProfile }>;
  lessons?: TimetableLesson[];
  lesson_instances?: Lesson[];
}

export interface LessonWithRelations extends Lesson {
  timetable?: TimetableWithRelations;
  whiteboards?: Array<LessonWhiteboard & { whiteboard: { id: string; name: string } }>;
}

export interface EnrollmentRequestWithProfile extends EnrollmentRequest {
  student?: UserProfile;
  class?: Class;
}

// ============================================================================
// Request/Response Types
// ============================================================================

export interface CreateClassRequest {
  name: string;
  subject: string;
  school_year: string;
  academic_term: string;
  description?: string;
}

export interface UpdateClassRequest {
  name?: string;
  subject?: string;
  school_year?: string;
  academic_term?: string;
  description?: string;
  is_active?: boolean;
}

export interface CreateTimetableRequest {
  class_id: string;
  name: string;
  type: TimetableType;
  start_date: string;
  end_date: string;
  recurrence_rule?: string;
}

export interface UpdateTimetableRequest {
  name?: string;
  start_date?: string;
  end_date?: string;
  recurrence_rule?: string;
  is_active?: boolean;
}

export interface CreateTimetableLessonRequest {
  day_of_week: DayOfWeek;
  start_time: string;
  end_time: string;
  room?: string;
  max_students?: number;
}

export interface UpdateTimetableLessonRequest {
  day_of_week?: DayOfWeek;
  start_time?: string;
  end_time?: string;
  room?: string;
  max_students?: number;
}

export interface GenerateLessonsRequest {
  start_date: string;
  end_date: string;
}

export interface CancelLessonRequest {
  reason: string;
}

export interface EnrollmentResponseRequest {
  status: 'approved' | 'rejected';
  response_message?: string;
}

export interface CreateEnrollmentRequest {
  message?: string;
}

// ============================================================================
// Filter and Pagination Types
// ============================================================================

export interface ClassFilters {
  subject?: string;
  school_year?: string;
  academic_term?: string;
  search?: string;
  skip?: number;
  limit?: number;
}

export interface TimetableFilters {
  class_id?: string;
  type?: TimetableType;
  is_active?: boolean;
  skip?: number;
  limit?: number;
}

export interface LessonFilters {
  timetable_id?: string;
  start_date?: string;
  end_date?: string;
  status?: LessonStatus;
  skip?: number;
  limit?: number;
}

export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  skip: number;
  limit: number;
}

