// API 接口类型定义文件

// 通用响应类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  timestamp?: string
}

// 分页响应类型
export interface PaginationResponse<T> {
  list: T[]
  total: number
  page: number
  pageSize: number
  totalPages: number
}

// 用户相关类型
export interface User {
  id: number
  username: string
  email: string
  phone?: string
  nickname?: string
  avatar?: string
  role: 'student' | 'teacher' | 'admin'
  status: 'active' | 'inactive' | 'banned'
  createdAt: string
  updatedAt: string
  profile?: UserProfile
}

export interface UserProfile {
  realName?: string
  gender?: 'male' | 'female' | 'other'
  birthday?: string
  bio?: string
  location?: string
  website?: string
  socialLinks?: {
    wechat?: string
    qq?: string
    weibo?: string
  }
}

// 登录注册类型
export interface LoginRequest {
  email?: string
  phone?: string
  password: string
  captcha?: string
}

export interface LoginResponse {
  user: User
  token: string
  refreshToken: string
  expiresIn: number
}

// 后端实际返回的登录响应格式
export interface BackendLoginResponse {
  token: string
  id: number
  timestamp: number
  expires: string
}

export interface RegisterRequest {
  username: string
  email: string
  phone?: string
  password: string
  confirmPassword: string
  captcha: string
  inviteCode?: string
}

// 课程相关类型
export interface Course {
  id: number
  title: string
  subtitle?: string
  description: string
  content?: string
  thumbnail: string
  coverImage?: string
  videoUrl?: string
  price: number
  originalPrice?: number
  discountPrice?: number
  currency: string
  rating: number
  ratingCount: number
  studentsCount: number
  duration: string
  totalLessons: number
  level: 'beginner' | 'intermediate' | 'advanced'
  language: string
  category: CourseCategory
  tags: string[]
  skills: string[]
  requirements: string[]
  objectives: string[]
  instructor: Instructor
  status: 'draft' | 'published' | 'archived'
  isEnrolled?: boolean
  progress?: number
  isFavorite?: boolean
  createdAt: string
  updatedAt: string
  publishedAt?: string
}

// 后端实际返回的课程数据格式
export interface BackendCourse {
  id: number
  name: string
  cover: string
  categoryId: number
  video: string
  school: string
  description: string
  target: string
  outline: string
  prerequisite: string
  reference: string
  arrangement: string
  startTime: string
  endTime: string
  revision: number
  question: string
  createdBy: number
  createdTime: string | null
  updatedBy: number
  updatedTime: string | null
  // 可选字段，根据API文档可能存在
  difficulty?: number // 难度等级
  subject?: string // 学科/主题
  price?: string
  teacherId?: number
  position?: string
}

// 后端课程列表响应格式
export interface BackendCourseListResponse {
  list: BackendCourse[]
  total: number
}

// 课程列表请求参数
export interface CourseListRequest {
  page?: number
  pageSize?: number
  categoryId?: number
  keyword?: string
  priceMin?: number
  priceMax?: number
  sortBy?: 'price' | 'startTime' | 'createdAt'
  sortOrder?: 'asc' | 'desc'
  difficulty?: number // 难度等级：0=简单，1=中等，2=困难
  subject?: string // 学科/主题
}

export interface CourseCategory {
  id: number
  name: string
  slug: string
  description?: string
  icon?: string
  parentId?: number
  children?: CourseCategory[]
}

export interface Instructor {
  id: number
  name: string
  title: string
  bio: string
  avatar: string
  rating: number
  studentsCount: number
  coursesCount: number
  experience: string
  education: string[]
  certifications: string[]
  socialLinks?: {
    website?: string
    linkedin?: string
    twitter?: string
  }
}

// 课程章节类型
export interface Chapter {
  id: number
  courseId: number
  title: string
  description?: string
  order: number
  duration: string
  isPublished: boolean
  lessons: Lesson[]
}

export interface Lesson {
  id: number
  chapterId: number
  courseId: number
  title: string
  description?: string
  content?: string
  videoUrl?: string
  duration: string
  order: number
  type: 'video' | 'text' | 'quiz' | 'assignment'
  isCompleted?: boolean
  isFree: boolean
  isPublished: boolean
  resources?: LessonResource[]
  quiz?: Quiz
  createdAt: string
  updatedAt: string
}

export interface LessonResource {
  id: number
  lessonId: number
  title: string
  description?: string
  type: 'pdf' | 'doc' | 'video' | 'audio' | 'image' | 'link'
  url: string
  size?: number
  downloadable: boolean
}

// 后端API返回的章节数据结构
export interface BackendCourseSection {
  id: number
  lessonId: number
  videoUrl: string // 视频链接
  name: string // 章节名称
  sortOrder: number // 排序
  parentId: number // 父章节ID
  level: number // 层级：0=子级（课时），1=父级（章节）
  revision: number // 版本号
  createdBy: number
  createdTime: string | null
  updatedBy: number
  updatedTime: string | null
}

// 前端使用的课程章节类型（适配后的数据结构）
export interface CourseSection {
  id: number
  lessonId: number
  outline: string // 章节大纲/内容链接（从videoUrl适配）
  name: string // 章节名称
  parentId: number // 父章节ID
  sort: number // 排序（从sortOrder适配）
  level: number // 层级：0=父级（章节），1=子级（课时）- 已从后端数据转换
  revision: number // 版本号
  createdAt: number | null // 从createdTime适配
  updatedAt: number | null // 从updatedTime适配
  deletedAt: string | null
  completed?: boolean // 是否已完成（前端状态）
  duration?: string // 课时时长（前端计算）
}

// 后端章节列表响应格式
export interface BackendCourseSectionListResponse {
  list: BackendCourseSection[]
  total: number
}

// 前端章节列表响应格式
export interface CourseSectionListResponse {
  list: CourseSection[]
  timestamp: number
  traceId: string
}

// 测验类型
export interface Quiz {
  id: number
  lessonId: number
  title: string
  description?: string
  timeLimit?: number
  passingScore: number
  questions: QuizQuestion[]
}

export interface QuizQuestion {
  id: number
  quizId: number
  question: string
  type: 'single' | 'multiple' | 'text' | 'essay'
  options?: string[]
  correctAnswer?: string | string[]
  explanation?: string
  points: number
  order: number
}

// 学习进度类型
export interface LearningProgress {
  id: number
  userId: number
  courseId: number
  lessonId?: number
  progress: number
  timeSpent: number
  lastAccessedAt: string
  completedAt?: string
  status: 'not_started' | 'in_progress' | 'completed'
}

// 评论类型
export interface Comment {
  id: number
  userId: number
  courseId?: number
  lessonId?: number
  parentId?: number
  content: string
  rating?: number
  likes: number
  dislikes: number
  isLiked?: boolean
  isDisliked?: boolean
  user: {
    id: number
    username: string
    avatar?: string
  }
  replies?: Comment[]
  createdAt: string
  updatedAt: string
}

// 收藏类型
export interface Favorite {
  id: number
  userId: number
  courseId: number
  course: Course
  createdAt: string
}

// 订单类型
export interface Order {
  id: number
  userId: number
  orderNo: string
  totalAmount: number
  discountAmount: number
  finalAmount: number
  currency: string
  status: 'pending' | 'paid' | 'cancelled' | 'refunded'
  paymentMethod?: string
  paymentTime?: string
  items: OrderItem[]
  createdAt: string
  updatedAt: string
}

export interface OrderItem {
  id: number
  orderId: number
  courseId: number
  course: Course
  price: number
  discountPrice?: number
}

// 搜索类型
export interface SearchRequest {
  keyword?: string
  category?: string
  level?: string
  price?: 'free' | 'paid' | 'all'
  rating?: number
  duration?: string
  language?: string
  sortBy?: 'newest' | 'oldest' | 'rating' | 'price_low' | 'price_high' | 'popular'
  page?: number
  pageSize?: number
}

// 统计类型
export interface Statistics {
  totalCourses: number
  totalStudents: number
  totalInstructors: number
  totalHours: number
  popularCategories: Array<{
    category: string
    count: number
  }>
  recentEnrollments: Array<{
    date: string
    count: number
  }>
}
