/**
 * Education Module Types
 * Type definitions for courses, lessons, progress, and gamification
 */

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

export type DifficultyLevel = 'beginner' | 'intermediate' | 'advanced';
export type CourseStatus = 'draft' | 'published' | 'archived';
export type ContentType = 'video' | 'text' | 'quiz' | 'exercise';
export type EnrollmentStatus = 'active' | 'completed' | 'paused' | 'cancelled';
export type QuestionType = 'multiple_choice' | 'multiple_answer' | 'true_false' | 'short_answer';

// ============================================================================
// Category Types
// ============================================================================

export interface CourseCategory {
  id: string;
  name: string;
  slug: string;
  description?: string;
  icon?: string;
  courseCount?: number;
}

// ============================================================================
// Instructor Types
// ============================================================================

export interface CourseInstructor {
  id: string;
  name: string;
  email?: string;
  avatar?: string;
  bio?: string;
  title?: string;
  totalCourses?: number;
  totalStudents?: number;
  avgRating?: number;
}

// ============================================================================
// Course Types
// ============================================================================

export interface CourseListItem {
  id: string;
  title: string;
  slug: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  difficultyLevel: DifficultyLevel;
  isFree: boolean;
  priceUsd?: number;
  category?: CourseCategory;
  instructorName?: string;
  instructorAvatar?: string;
  totalLessons: number;
  totalDuration: number;
  avgRating: number;
  totalReviews: number;
  totalEnrollments: number;
  xpReward: number;
  createdAt: string;
  updatedAt: string;
}

export interface CourseDetail extends CourseListItem {
  description?: string;
  learningObjectives: string[];
  requirements: string[];
  instructor?: CourseInstructor;
  modules: CourseModule[];
  userEnrollment?: UserEnrollment;
  tags: string[];
}

export interface CourseModule {
  id: string;
  courseId: string;
  title: string;
  description?: string;
  sortOrder: number;
  lessons: LessonListItem[];
  totalDuration: number;
  totalLessons: number;
  isLocked?: boolean;
}

// ============================================================================
// Lesson Types
// ============================================================================

export interface LessonListItem {
  id: string;
  moduleId: string;
  title: string;
  description?: string;
  contentType: ContentType;
  durationMinutes: number;
  sortOrder: number;
  isFree?: boolean;
  isCompleted?: boolean;
  watchPercentage?: number;
}

export interface LessonDetail extends LessonListItem {
  courseId: string;
  videoUrl?: string;
  contentHtml?: string;
  contentMarkdown?: string;
  resources: LessonResource[];
  userProgress?: LessonProgress;
  previousLesson?: { id: string; title: string };
  nextLesson?: { id: string; title: string };
  courseTitle: string;
  courseSlug: string;
  moduleTitle: string;
  xpReward?: number;
}

export interface LessonResource {
  name: string;
  url: string;
  type: string;
}

export interface LessonProgress {
  id: string;
  lessonId: string;
  enrollmentId: string;
  isCompleted: boolean;
  videoWatchedSeconds: number;
  videoCompleted: boolean;
  completedAt?: string;
  updatedAt: string;
}

// ============================================================================
// Enrollment Types
// ============================================================================

export interface UserEnrollment {
  id: string;
  userId: string;
  courseId: string;
  status: EnrollmentStatus;
  progressPercentage: number;
  completedLessons: number;
  totalLessons: number;
  lastAccessedAt?: string;
  completedAt?: string;
  enrolledAt: string;
  certificateIssued?: boolean;
  certificateUrl?: string;
}

export interface EnrollmentWithCourse extends UserEnrollment {
  course: CourseListItem;
}

// ============================================================================
// Quiz Types
// ============================================================================

export interface Quiz {
  id: string;
  lessonId?: string;
  courseId: string;
  title: string;
  description?: string;
  passingScore: number;
  timeLimitMinutes?: number;
  maxAttempts?: number;
  shuffleQuestions: boolean;
  showCorrectAnswers: boolean;
  questions: QuizQuestion[];
}

export interface QuizQuestion {
  id: string;
  quizId: string;
  questionText: string;
  questionType: QuestionType;
  points: number;
  explanation?: string;
  options?: QuizOption[];
  sortOrder: number;
}

export interface QuizOption {
  id: string;
  text: string;
  isCorrect?: boolean;
}

export interface QuizAttempt {
  id: string;
  quizId: string;
  userId: string;
  score: number;
  passed: boolean;
  timeSpentSeconds?: number;
  answers: QuizAnswer[];
  startedAt: string;
  submittedAt?: string;
}

export interface QuizAnswer {
  questionId: string;
  answer: string | string[];
  isCorrect: boolean;
}

export interface QuizResult {
  attempt: QuizAttempt;
  score: number;
  maxScore: number;
  percentage: number;
  passed: boolean;
  results?: QuestionResult[];
  xpAwarded?: number;
}

export interface QuestionResult {
  questionId: string;
  isCorrect: boolean;
  pointsEarned: number;
  maxPoints: number;
  userAnswer: string | string[];
  correctAnswer?: string | string[];
  feedback: string;
}

// ============================================================================
// Gamification Types
// ============================================================================

export interface GamificationProfile {
  id: string;
  userId: string;
  totalXp: number;
  currentLevel: number;
  xpToNextLevel: number;
  currentStreakDays: number;
  longestStreakDays: number;
  weeklyXp: number;
  monthlyXp: number;
  totalCoursesCompleted: number;
  totalLessonsCompleted: number;
  totalQuizzesPassed: number;
  averageQuizScore: number;
  levelProgress?: LevelProgress;
}

export interface LevelProgress {
  currentLevel: number;
  totalXp: number;
  xpCurrentLevel: number;
  xpNextLevel: number;
  xpIntoLevel: number;
  xpNeeded: number;
  progressPercentage: number;
}

export interface Achievement {
  id: string;
  achievementType: string;
  title: string;
  description: string;
  badgeIconUrl?: string;
  xpBonus: number;
  rarity?: 'common' | 'uncommon' | 'rare' | 'epic' | 'legendary';
  earnedAt: string;
}

export interface StreakStats {
  currentStreak: number;
  longestStreak: number;
  lastActivity: string | null;
  nextMilestone: number;
  daysToMilestone: number;
}

export interface LeaderboardEntry {
  rank: number;
  userId: string;
  userName: string;
  avatarUrl: string | null;
  totalXp: number;
  currentLevel: number;
  coursesCompleted: number;
  currentStreak: number;
}

export interface UserLeaderboardPosition {
  rank: number | null;
  totalUsers: number;
  percentile: number;
}

export interface GamificationSummary {
  profile: {
    totalXp: number;
    currentLevel: number;
    weeklyXp: number;
    monthlyXp: number;
  };
  levelProgress: LevelProgress;
  streak: StreakStats;
  achievements: {
    total: number;
    recent: Achievement[];
  };
  leaderboard: UserLeaderboardPosition;
  stats: {
    coursesCompleted: number;
    lessonsCompleted: number;
    quizzesPassed: number;
    averageQuizScore: number;
  };
}

// ============================================================================
// Filter Types
// ============================================================================

export interface CourseFilters {
  categoryId?: string;
  level?: DifficultyLevel;
  isFree?: boolean;
  search?: string;
  minRating?: number;
  sortBy?: 'newest' | 'popular' | 'rating' | 'price_asc' | 'price_desc';
  page?: number;
  pageSize?: number;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface PaginatedResponse<T> {
  data: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export interface ApiResponse<T> {
  success: boolean;
  data: T;
  error?: {
    message: string;
    code: string;
  };
}
