import type { Timestamp, FieldValue } from '@firebase/firestore';
import type { FunctionalComponent } from 'vue';
import type { HTMLAttributes, VNodeProps } from 'vue';
//FIrebase
import type { FirebaseApp } from 'firebase/app';
import type { Auth, RecaptchaVerifier } from 'firebase/auth';
import type { Firestore, CollectionReference } from 'firebase/firestore';
import type { FirebaseStorage } from 'firebase/storage';
import type { Functions } from 'firebase/functions';
import type { Analytics } from 'firebase/analytics';
import type { CourseCategory, ProgramCategory, VideoProvider } from './config';

export interface FirebasePlugin {
  firebaseApp: FirebaseApp;
  auth: Auth;
  db: Firestore;
  storage: FirebaseStorage;
  functions: Functions;
  analytics: Analytics;
  usersCollection: CollectionReference;
  faqboardsCollection: CollectionReference;
  countersCollection: CollectionReference;
  attendsCollection: CollectionReference;
  noticesCollection: CollectionReference;
  videosCollection: CollectionReference;
  quizzesCollection: CollectionReference;
  coursesCollection: CollectionReference;
  progressesCollection: CollectionReference;
  usergroupsCollection: CollectionReference;
  surveysCollection: CollectionReference;
  questionsCollection: CollectionReference;
  programsCollection: CollectionReference;
  headersCollection: CollectionReference;
  certificatesCollection: CollectionReference;
  projectsCollection: CollectionReference;
  signInWithGoogle: () => Promise<any>;
  createRecaptchaVerifier: (elementId: string) => RecaptchaVerifier;
}
export interface FirebaseConfig {
  apiKey: string;
  authDomain: string;
  projectId: string;
  storageBucket: string;
  messagingSenderId: string;
  appId: string;
}
// Management Page

//config
export type ResizedUrls = {
  w200?: string;
  w500?: string;
};

// Management Page
export interface NavigationItem {
  name: string;
  route: any;
  icon: any; // Use a more specific type if available.
  current: boolean;
}

// Board : Elements
export type ReservationSubmission = {
  docId: string;
  userId: string;
  paymentId: string;
  name: string;
  address: string;
  email: string;
  phone: string;
  emergencyPhone: string;
  scheduleStart: string;
  scheduleEnd: string | null;
  attending: boolean;
  altName?: string | null;
  altPhone?: string | null;
  companions?: string;
  healthNotes?: string;
  remarks?: string;
  submittedAt: Date;
  boardState?: { state: 'processing' | 'completed' };
};

export type FileItem = {
  name: string;
  url: string;
};
export type ThumbnailItem = FileItem & {
  fileName?: string;
  originalUrl?: string;
  resizedUrls?: ResizedUrls;
};
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
}

export interface ThumbnailData {
  input: Ref<File | null>;
  preview: Ref<string>;
  deleteTrigger: Ref<boolean>;
  oldPreviewState: Ref<boolean>;
  uploadState: Ref<boolean>;
  previewState: Ref<boolean>;
  displayedName: Ref<string>;
}

export interface UploadFileData {
  input: File | null;
  preview: string;
  deleteTrigger: boolean;
  oldPreviewState: boolean;
  uploadState: boolean;
  previewState: boolean;
  displayedName: string;
}

export interface UploadsData {
  thumbnail: ThumbnailData;
  files: UploadFileData[];
}
// Board : Finally
export type BoardItem = {
  docId: string;
  userId: string;
  title: string;
  description: string;
  announcement?: boolean;
  ishidden?: boolean;
  boards_number: number;
  created: Timestamp | string | FieldValue;
  name?: string;
  files?: FileItem[];
  thumbnail?: ThumbnailItem;

  // add other properties if needed
};
// 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;
}

export interface UserNavigation {
  name: string;
  route: { name: string };
  onClick?: () => void; // Add an optional onClick property for handling the click event
}
export interface CreateProgressResponse {
  docId: string;
}

export interface CertificateResponse {
  status: 'success' | 'failed';
  message?: {
    imageUrl: string;
    pdfUrl: string;
    uniqueId: string;
  };
}

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

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

//user
export type UserRegistrationValues = {
  email: string;
  name: string;
  password: string;
  confirm_password: string;
  membership:
    | '당사자'
    | '동료지원가'
    | '당사자 지인 또는 가족'
    | '기관종사자'
    | '일반시민';
  tos: boolean;
};
export interface LoginValues {
  email: string;
  password: string;
}

export interface VimeoPlaybackData {
  duration: number;
  percent: number;
}

//Tests
export interface CompData {
  title: string;
}

export interface Product {
  name: string;
  description: string;
  href: string;
  icon: FunctionalComponent<HTMLAttributes & VNodeProps, any>;
}

export interface CallToAction {
  name: string;
  href: string;
  icon: FunctionalComponent<HTMLAttributes & VNodeProps, any>;
}
//Funtion Types

export type UpdateDisplayedItemsFn = (
  currentItems: Ref<BoardItem[]>,
  displayedItems: Ref<BoardItem[]>,
  currentPage: Ref<number>,
  itemsPerPage: number
) => void;
