import type { Timestamp } from 'firebase-admin/firestore';
import type { CourseCategory, ProgramCategory, VideoProvider } from '../config';

export type BoardAccessMode = 'public' | 'private' | 'admin';
export interface CursorResponse<T> {
  items: T[];
  nextPageToken: string | null;
}
export interface UseBoardListOptions {
  title?: string;
  itemsPerPage?: number;
  defaultSort?: OrderByDirection;
  access?: BoardAccessMode;
  loadingMessage?: string;
}
//config

// Management Page
/////////////////////////Board Section ////////////////////////////
export type boardState = {
  state: 'processing' | 'pending' | 'queued' | 'completed' | 'error';
  error?: string;
};

// Board : Elements

export type FileItem = {
  name: string;
  url: string;
  uuid?: string; // ? for old data structure
};

export type ImageUsage = {
  boardId: string;
  type: 'ckeditor' | 'thumbnail';
};
export type ResizeOptions = 'w200' | 'w500' | 'w1280';

export type ResizedImageVersion = {
  url: string; // Publicly accessible HTTPS download URL for this WebP version.
  path: string; // Full path in Firebase Storage to this WebP version.
  width: number; // Actual width of this WebP version after resizing.
  height: number; // Actual height of this WebP version after resizing.
  format: 'webp'; // Explicitly 'webp' as all resized versions will be this format.
  label: ResizeOptions; // The label used in the filename, e.g., "200x200", "500x500".
};
export type ResizeStatus =
  | 'pending_resize'
  | 'resizing'
  | 'resized'
  | 'resize_error'
  | 'archived';
export type ImageAsset = {
  uuid: string; // UUID of the image asset and as docID
  storagePath: string; // e.g., "boards/general/board123/ckeditor/uuid-abc/original/myphoto.jpg"
  originalFileNameWithExt: string; // e.g., "myphoto.jpg"
  userId: string; // ID of the user who uploaded the image.
  createdAt: Timestamp;
  boardReferences: ImageUsage[];
  originalSize?: {
    width: number;
    height: number;
    format?: string; // .jpg, .png, etc.
  };
  resizedVersions?: ResizedImageVersion[];
  lastProcessedAt?: Timestamp; // resize function last processed this image.
  status: ResizeStatus;
  processingError?: string; // Stores an error message if 'status' is 'resize_error'.
};

export type ImageItem = FileItem; // Depreciated : Original Image Item
// depreciated : old data structure
export interface UploadFilesOptions {
  newBoardData: BoardItem;
  newThumbnail: File | null;
  newFiles: File[];
  oldThumbnailUrl?: string;
  oldFiles?: FileItem[];
  deleteThumbnail?: boolean;
  deleteFileIndexes?: number[];
  currentBoard: string; // which indices of old files to delete
}
// depreciated : old data structure
export interface UploadFileData {
  input: File | null;
  preview: string;
  deleteTrigger: boolean;
  oldPreviewState: boolean;
  uploadState: boolean;
  previewState: boolean;
  displayedName: string;
}
// Board : Finally
export type BoardItem = {
  //metadata
  docId: string;
  userId: string;
  title: string;
  created: Timestamp | string;
  boardState: boardState;
  //contents
  description: string;
  announcement?: boolean;
  //files
  files?: FileItem[];
  thumbnailUuids?: string; //New field for thumbnailAssetUuid
  ckAssetUuids?: string[]; // Stores an array of imageAssetUuids used in description
  //depricated
  boards_number?: number; //depriciated
  ishidden?: boolean; // depriciated, we can use this for later admin control
  thumbnail?: ImageItem; //Leave it for previous data structure
};
// Board : Types
export interface ProjectBoard extends BoardItem {
  subtitle: string;
  displayDate?: string;
  author?: string;
}

export interface QuizBoard extends BoardItem {
  question: string;
  answers: {
    text: string;
    correct: boolean;
  }[];
}

export interface VideoInfo {
  url: string;
  duration: number;
  provider: VideoProvider;
  vimeoId?: string;
}
export interface VideoBoard extends BoardItem {
  video: VideoInfo;
}
export interface CourseBoard extends BoardItem {
  category: CourseCategory;
  isStrict: boolean;
  courseElements?: CourseElement[];
  headline?: string;
  video?: VideoInfo;
  expiresWhen?: string;
  keywords?: string[];
}

// Board : Types Elements
export interface CourseElement {
  docId: string;
  order: number;
  title: string;
  type: CourseElementType;
  duration?: number;
}
export interface CategoryItem {
  id: number;
  name: string;
}
export type CourseElementType = 'video' | 'quiz' | 'docs';

// Program
export interface CourseInProgram {
  docId: string;
  title: string;
  order: number;
}

export interface ProgramBoard extends BoardItem {
  courses: CourseInProgram[];
  category: ProgramCategory;
  isStrict: boolean;
  headline?: string;
  video?: VideoInfo;
  expiresWhen?: string;
  keywords?: string[];
}

// past

export interface ProgramWithProgress extends ProgramBoard {
  progressPercentage: number;
  completed: boolean;
  lastCourseProgressDocId: string | null;
}

// Course
export interface Coursecreators {
  id: number;
  name: string;
  route: { name: string };
  initial: string;
  current: boolean;
}
export type CourseNavItem = {
  id: number;
  name: string;
  route: { name: string };
  current: boolean;
  docId: string;
  lessonFinish: boolean;
  type: string;
  progressId: string;
  courseId: string;
};

export interface Lesson {
  docId: string;
  order: number;
  title: string;
  type: string;
  duration?: number;
}

// Progress
export type Progress = {
  docId: string;
  boards_number: number;
  courseCategory: string;
  courseId: string;
  created: Timestamp;
  lessons: LessonProgress[]; // ✅ Fixed here
  userId: string;
  courseFinish: Timestamp;
  courseTitle: string;
  userSurveyed?: boolean;
  ishidden?: boolean;
  finishedDate?: Timestamp;
  complete?: boolean;
  extended?: boolean;
};
export type LessonProgress = {
  currentTime?: number;
  docId: string;
  duration?: number;
  lessonFinish: boolean;
  order: number;
  type: string;
  title: string;
};

export interface ProgressWithRuntimeData
  extends Omit<Progress, 'courseTitle' | 'courseCategory' | 'percentage'> {
  totalDuration?: number;
  currentTime?: number;
  percentage?: number;
  courseTitle: string;
  category?: string;
  courseThumbnail?: string;
  courseHeadline?: string;
  courseCategory: string;
}

// Nav
export type SortOption = {
  sortId: number;
  name: string;
  rule: OrderByDirection;
};

export type OrderByDirection = 'asc' | 'desc';
