/**
 * Global Type Definitions for Math Platform
 */

/**
 * User Types
 */
export interface User {
  id: string;
  email: string;
  username: string;
  createdAt: string;
  lastLoginAt: string;
}

export interface RegisterData {
  email: string;
  username: string;
  password: string;
  confirmPassword?: string;
}

export interface LoginData {
  email: string;
  password: string;
}

export interface AuthResponse {
  user: User;
  token: string;
}

/**
 * Module Types
 */
export type ModuleType = 'FUNDAMENTOS' | 'SISTEMAS' | 'APLICACIONES';

export type TopicType =
  | 'VECTORES'
  | 'MATRICES'
  | 'SISTEMAS'
  | 'ESPACIOS_VECTORIALES'
  | 'PROGRAMACION_LINEAL';

export interface Module {
  id: string;
  name: string;
  description: string;
  type: ModuleType;
  order: number;
  introduction: string | null;
  examples: ModuleContent[] | null;
  exercises: Exercise[] | null;
  answers: ModuleAnswer[] | null;
}

export interface ModuleAnswer {
  id: string;
  exerciseId: string;
  content: string;
  steps: string[];
}

export interface ModuleContent {
  id: string;
  title: string;
  content: string;
  formulas?: string[];
  examples?: string[];
}

/**
 * Topic Types
 */
export interface Topic {
  id: string;
  moduleId: string;
  name: string;
  type: TopicType;
  theoryContent: TheoryContent | null;
  formulas: Formula[] | null;
}

export interface TheoryContent {
  title: string;
  description: string;
  sections: TheorySection[];
}

export interface TheorySection {
  title: string;
  content: string;
  formulas?: string[];
}

export interface Formula {
  id: string;
  name: string;
  latex: string;
  description?: string;
}

/**
 * Exercise Types
 */
export type ExerciseType = 'MULTIPLE_CHOICE' | 'OPEN_ENDED' | 'TRUE_FALSE' | 'CALCULATION';
export type ExerciseDifficulty = 'EASY' | 'MEDIUM' | 'HARD';

export interface Exercise {
  id: string;
  moduleId: string;
  topicId: string | null;
  type: ExerciseType;
  difficulty: ExerciseDifficulty;
  statement: string;
  correctAnswer: string;
  solutionSteps: SolutionStep[];
  formulas: string[];
  isAIGenerated: boolean;
  points: number;
  hints?: string[];
  options?: ExerciseOption[];
}

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

export interface SolutionStep {
  step: number;
  description: string;
  formula?: string;
}

export interface ExerciseAttempt {
  id: string;
  userId: string;
  exerciseId: string;
  userAnswer: string;
  status: 'CORRECT' | 'INCORRECT' | 'PARTIAL';
  pointsEarned: number;
  timeSpentSeconds: number;
  attemptedAt: string;
}

export interface SubmitAttemptData {
  userAnswer: string;
  timeSpentSeconds: number;
}

/**
 * Progress Types
 */
export interface Progress {
  userId: string;
  moduleId: string;
  exercisesCompleted: number;
  totalExercises: number;
  points: number;
  percentage: number;
  isStarted: boolean;
  isCompleted: boolean;
  lastActivityAt: string;
}

/**
 * Achievement Types
 */
export type AchievementCategory =
  | 'EXERCISES'
  | 'MODULES'
  | 'STREAKS'
  | 'RANKING'
  | 'SPECIAL';

export type AchievementRarity = 'COMMON' | 'RARE' | 'EPIC' | 'LEGENDARY';

export interface Achievement {
  code: string;
  name: string;
  description: string;
  icon: string;
  rarity: AchievementRarity;
  category: AchievementCategory;
  requirementType: string;
  requirementValue: number;
}

export interface UserAchievement {
  userId: string;
  achievementCode: string;
  progress: number;
  requirementValue: number;
  unlockedAt: string | null;
}

/**
 * Ranking Types
 */
export interface RankingEntry {
  position: number;
  points: number;
  exercisesCompleted: number;
  streak: number;
  badges: AchievementBadge[];
}

export interface AchievementBadge {
  code: string;
  icon: string;
  rarity: AchievementRarity;
}

export interface GlobalRanking {
  global: RankingEntry[];
  byModule: Record<string, RankingEntry[]>;
  myPosition: {
    global: RankingEntry | null;
    byModule: Record<string, RankingEntry | null>;
  };
}

/**
 * Notification Types (Backend Only)
 */
export interface Notification {
  id: string;
  type: 'INFO' | 'SUCCESS' | 'WARNING' | 'ERROR';
  title: string;
  message: string;
  telegramChatId: string;
  isSent: boolean;
  sentAt: string | null;
  createdAt: string;
}

/**
 * API Response Types
 */
export interface ApiErrorResponse {
  code: string;
  message: string;
  details?: unknown;
}

export interface ApiResponse<T> {
  data?: T;
  message?: string;
  error?: string | ApiErrorResponse;
  meta?: {
    timestamp: string;
    requestId?: string;
  };
}

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

/**
 * Form Types
 */
export interface FormFieldError {
  field: string;
  message: string;
}

export interface FormState<T> {
  values: T;
  errors: FormFieldError[];
  touched: Record<keyof T, boolean>;
  isSubmitting: boolean;
  isValid: boolean;
}

/**
 * UI Component Types
 */
export interface Tab {
  id: string;
  label: string;
  content: React.ReactNode;
  disabled?: boolean;
}

export interface BreadcrumbItem {
  label: string;
  href?: string;
  icon?: React.ReactNode;
}

export interface SelectOption {
  value: string;
  label: string;
  disabled?: boolean;
}

/**
 * Theme Types
 */
export type Theme = 'light' | 'dark' | 'system';

/**
 * Filter and Sort Types
 */
export interface FilterOptions {
  search?: string;
  difficulty?: ExerciseDifficulty[];
  type?: ExerciseType[];
  moduleId?: string[];
  topicId?: string[];
}

export interface SortOptions {
  field: 'createdAt' | 'points' | 'difficulty' | 'name';
  order: 'asc' | 'desc';
}

/**
 * Statistics Types
 */
export interface UserStatistics {
  totalExercisesCompleted: number;
  totalPoints: number;
  averageScore: number;
  totalStudyTime: number;
  currentStreak: number;
  longestStreak: number;
  modulesCompleted: number;
  achievementsUnlocked: number;
  rankGlobal: number | null;
}

export interface ModuleStatistics {
  exercisesCompleted: number;
  totalExercises: number;
  points: number;
  averageScore: number;
  timeSpent: number;
  lastActivityAt: string;
}
