import { books, students, borrowRecords, reservations, fines, reviews, admins } from '@/lib/db/schema';

// 数据库表类型
export type Book = typeof books.$inferSelect;
export type NewBook = typeof books.$inferInsert;

export type Student = typeof students.$inferSelect;
export type NewStudent = typeof students.$inferInsert;

export type BorrowRecord = typeof borrowRecords.$inferSelect;
export type NewBorrowRecord = typeof borrowRecords.$inferInsert;

export type Reservation = typeof reservations.$inferSelect;
export type NewReservation = typeof reservations.$inferInsert;

export type Fine = typeof fines.$inferSelect;
export type NewFine = typeof fines.$inferInsert;

export type Review = typeof reviews.$inferSelect;
export type NewReview = typeof reviews.$inferInsert;

export type Admin = typeof admins.$inferSelect;
export type NewAdmin = typeof admins.$inferInsert;

// 扩展类型，包含关联数据
export type BookWithDetails = Book & {
  reviews?: Review[];
  borrowCount?: number;
  averageRating?: number;
};

export type StudentWithDetails = Student & {
  borrowRecords?: BorrowRecord[];
  reservations?: Reservation[];
  fines?: Fine[];
  totalFines?: number;
};

export type BorrowRecordWithDetails = BorrowRecord & {
  book?: Book;
  student?: Student;
};

export type ReservationWithDetails = Reservation & {
  book?: Book;
  student?: Student;
};

export type FineWithDetails = Fine & {
  student?: Student;
  borrowRecord?: BorrowRecord;
};

// 用户会话类型
export interface UserSession {
  id: string;
  name: string;
  email?: string;
  role: 'admin' | 'student';
  studentId?: number;
  adminId?: number;
}

// API 响应类型
export interface ApiResponse<T = unknown> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
}

// 搜索过滤器类型
export interface BookSearchFilters {
  title?: string;
  author?: string;
  isbn?: string;
  classificationNo?: string;
  status?: 'normal' | 'damaged' | 'removed';
  available?: boolean;
}

export interface StudentSearchFilters {
  name?: string;
  stuNo?: string;
  department?: string;
  major?: string;
  accountStatus?: 'active' | 'frozen' | 'reported';
}

// 统计数据类型
export interface LibraryStats {
  totalBooks: number;
  totalStudents: number;
  totalBorrows: number;
  overdueBooks: number;
  reservations: number;
  unpaidFines: number;
}

// 分页类型
export interface PaginationParams {
  page: number;
  pageSize: number;
}

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