/**
 * Education Module Types
 */

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

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

// ============================================================================
// Category
// ============================================================================

export interface Category {
  id: string;
  name: string;
  slug: string;
  description?: string;
  icon?: string;
  parentId?: string;
  sortOrder: number;
  createdAt: Date;
  updatedAt: Date;
}

export interface CreateCategoryInput {
  name: string;
  slug?: string;
  description?: string;
  icon?: string;
  parentId?: string;
  sortOrder?: number;
}

// ============================================================================
// Course
// ============================================================================

export interface Course {
  id: string;
  title: string;
  slug: string;
  description?: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  previewVideoUrl?: string;
  categoryId?: string;
  level: CourseLevel;
  tags: string[];
  isFree: boolean;
  price: number;
  currency: string;
  requiresSubscription: boolean;
  minSubscriptionTier?: string;
  durationMinutes?: number;
  lessonsCount: number;
  enrolledCount: number;
  averageRating: number;
  ratingsCount: number;
  status: CourseStatus;
  publishedAt?: Date;
  instructorId?: string;
  aiGenerated: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface CourseWithDetails extends Course {
  category?: Category;
  instructor?: {
    id: string;
    email: string;
    profile?: {
      firstName?: string;
      lastName?: string;
      avatarUrl?: string;
    };
  };
  modules?: ModuleWithLessons[];
}

export interface CreateCourseInput {
  title: string;
  slug?: string;
  description?: string;
  shortDescription?: string;
  thumbnailUrl?: string;
  previewVideoUrl?: string;
  categoryId?: string;
  level?: CourseLevel;
  tags?: string[];
  isFree?: boolean;
  price?: number;
  currency?: string;
  requiresSubscription?: boolean;
  minSubscriptionTier?: string;
  durationMinutes?: number;
  instructorId?: string;
}

export interface UpdateCourseInput extends Partial<CreateCourseInput> {
  status?: CourseStatus;
}

export interface CourseFilters {
  categoryId?: string;
  level?: CourseLevel;
  status?: CourseStatus;
  isFree?: boolean;
  instructorId?: string;
  search?: string;
  minRating?: number;
  tags?: string[];
}

// ============================================================================
// Module
// ============================================================================

export interface Module {
  id: string;
  courseId: string;
  title: string;
  description?: string;
  sortOrder: number;
  unlockAfterModuleId?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface ModuleWithLessons extends Module {
  lessons: Lesson[];
}

export interface CreateModuleInput {
  courseId: string;
  title: string;
  description?: string;
  sortOrder?: number;
  unlockAfterModuleId?: string;
}

// ============================================================================
// Lesson
// ============================================================================

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

export interface Lesson {
  id: string;
  moduleId: string;
  courseId: string;
  title: string;
  slug: string;
  contentType: ContentType;
  videoUrl?: string;
  videoDurationSeconds?: number;
  videoProvider?: string;
  contentMarkdown?: string;
  contentHtml?: string;
  resources: LessonResource[];
  sortOrder: number;
  isPreview: boolean;
  aiGenerated: boolean;
  aiSummary?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface LessonWithProgress extends Lesson {
  progress?: LessonProgress;
}

export interface CreateLessonInput {
  moduleId: string;
  courseId: string;
  title: string;
  slug?: string;
  contentType?: ContentType;
  videoUrl?: string;
  videoDurationSeconds?: number;
  videoProvider?: string;
  contentMarkdown?: string;
  resources?: LessonResource[];
  sortOrder?: number;
  isPreview?: boolean;
}

// ============================================================================
// Quiz
// ============================================================================

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

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

export interface Quiz {
  id: string;
  lessonId?: string;
  courseId: string;
  title: string;
  description?: string;
  passingScore: number;
  maxAttempts?: number;
  timeLimitMinutes?: number;
  shuffleQuestions: boolean;
  showCorrectAnswers: boolean;
  aiGenerated: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface QuizWithQuestions extends Quiz {
  questions: QuizQuestion[];
}

export interface CreateQuizInput {
  lessonId?: string;
  courseId: string;
  title: string;
  description?: string;
  passingScore?: number;
  maxAttempts?: number;
  timeLimitMinutes?: number;
  shuffleQuestions?: boolean;
  showCorrectAnswers?: boolean;
}

export interface CreateQuizQuestionInput {
  quizId: string;
  questionType?: QuestionType;
  questionText: string;
  explanation?: string;
  options?: Omit<QuizOption, 'id'>[];
  correctAnswers?: string[];
  points?: number;
  sortOrder?: number;
}

// ============================================================================
// Enrollment
// ============================================================================

export interface Enrollment {
  id: string;
  userId: string;
  courseId: string;
  status: EnrollmentStatus;
  progressPercentage: number;
  lessonsCompleted: number;
  enrolledAt: Date;
  expiresAt?: Date;
  completedAt?: Date;
  paymentId?: string;
  certificateIssued: boolean;
  certificateUrl?: string;
  certificateIssuedAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export interface EnrollmentWithCourse extends Enrollment {
  course: Course;
}

export interface CreateEnrollmentInput {
  userId: string;
  courseId: string;
  paymentId?: string;
  expiresAt?: Date;
}

// ============================================================================
// Lesson Progress
// ============================================================================

export interface LessonProgress {
  id: string;
  userId: string;
  lessonId: string;
  enrollmentId: string;
  videoWatchedSeconds: number;
  videoCompleted: boolean;
  startedAt?: Date;
  completedAt?: Date;
  userNotes?: string;
  createdAt: Date;
  updatedAt: Date;
}

export interface UpdateLessonProgressInput {
  videoWatchedSeconds?: number;
  videoCompleted?: boolean;
  userNotes?: string;
}

// ============================================================================
// Quiz Attempts
// ============================================================================

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

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

export interface SubmitQuizInput {
  quizId: string;
  answers: { questionId: string; answer: string | string[] }[];
}

// ============================================================================
// Reviews
// ============================================================================

export interface CourseReview {
  id: string;
  userId: string;
  courseId: string;
  rating: number;
  reviewText?: string;
  isApproved: boolean;
  isFeatured: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface CourseReviewWithUser extends CourseReview {
  user: {
    id: string;
    email: string;
    profile?: {
      firstName?: string;
      lastName?: string;
      avatarUrl?: string;
    };
  };
}

export interface CreateReviewInput {
  courseId: string;
  rating: number;
  reviewText?: string;
}

// ============================================================================
// Pagination
// ============================================================================

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

export interface PaginationOptions {
  page?: number;
  pageSize?: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}
