export interface Story {
  id: string;
  title: string;
  summary?: string;
  authorId: string;
  authorName: string;
  contentHtml: string;
  contentPlain?: string; // Optional - only included in reading/detail views
  sourceUrl?: string;
  wordCount: number;
  seriesId?: string;
  seriesName?: string;
  volume?: number;
  rating?: number;
  coverPath?: string;
  tags: Tag[];
  tagNames?: string[] | null; // Used in search results
  readingPosition?: number;
  readingProgressPercentage?: number; // Pre-calculated percentage (0-100) from backend
  lastReadAt?: string;
  lastCompletedAt?: string;
  createdAt: string;
  updatedAt: string;
}

export interface Author {
  id: string;
  name: string;
  notes?: string;
  authorRating?: number;
  averageStoryRating?: number;
  avatarImagePath?: string;
  urls: string[];
  storyCount: number;
  createdAt: string;
  updatedAt: string;
}

export interface AuthorUrl {
  id: string;
  url: string;
  description?: string;
}

export interface Tag {
  id: string;
  name: string;
  color?: string; // hex color like #3B82F6
  description?: string;
  storyCount?: number;
  collectionCount?: number;
  aliasCount?: number;
  aliases?: TagAlias[];
  createdAt?: string;
  updatedAt?: string;
}

export interface TagAlias {
  id: string;
  aliasName: string;
  canonicalTagId: string;
  canonicalTag?: Tag;
  createdFromMerge: boolean;
  createdAt: string;
}

export interface Series {
  id: string;
  name: string;
  storyCount: number;
}

export interface AuthResponse {
  token: string;
  expiresIn: number;
}

export interface FacetCount {
  value: string;
  count: number;
}

export interface SearchResult {
  results: Story[];
  totalHits: number;
  page: number;
  perPage: number;
  query: string;
  searchTimeMs: number;
  facets?: Record<string, FacetCount[]>;
}

export interface PagedResult<T> {
  content: T[];
  totalElements: number;
  totalPages: number;
  number: number;
  size: number;
  numberOfElements: number;
  first: boolean;
  last: boolean;
  empty: boolean;
}

export interface Collection {
  id: string;
  name: string;
  description?: string;
  rating?: number;
  coverImagePath?: string;
  isArchived: boolean;
  tags: Tag[];
  tagNames?: string[] | null; // Used in search results
  collectionStories?: CollectionStory[];
  stories?: CollectionStory[];  // For compatibility
  storyCount: number;
  totalWordCount?: number;
  estimatedReadingTime?: number;
  averageStoryRating?: number;
  createdAt: string;
  updatedAt: string;
}

export interface CollectionStory {
  story: Story;
  position: number;
  addedAt: string;
}

export interface CollectionSearchResult {
  results: Collection[];
  totalHits: number;
  page: number;
  perPage: number;
  query: string;
  searchTimeMs: number;
}

export interface CollectionReadingContext {
  id: string;
  name: string;
  currentPosition: number;
  totalStories: number;
  previousStoryId?: string;
  nextStoryId?: string;
}

export interface StoryWithCollectionContext {
  story: Story;
  collection: CollectionReadingContext;
}

export interface CollectionStatistics {
  totalStories: number;
  totalWordCount: number;
  estimatedReadingTime: number;
  averageStoryRating: number;
  averageWordCount: number;
  tagFrequency: Record<string, number>;
  authorDistribution: Array<{
    authorName: string;
    storyCount: number;
  }>;
}

// Advanced filter interfaces
export interface AdvancedFilters {
  // Word count filters
  minWordCount?: number;
  maxWordCount?: number;
  
  // Date filters
  createdAfter?: string; // ISO date string
  createdBefore?: string; // ISO date string
  lastReadAfter?: string;
  lastReadBefore?: string;
  
  // Rating filters (extending existing)
  minRating?: number;
  maxRating?: number;
  unratedOnly?: boolean;
  
  // Reading status filters
  readingStatus?: 'all' | 'unread' | 'started' | 'completed';
  hasReadingProgress?: boolean;
  
  // Content filters
  hasCoverImage?: boolean;
  sourceDomain?: string;
  
  // Series filters
  seriesFilter?: 'all' | 'standalone' | 'series' | 'firstInSeries' | 'lastInSeries';
  
  // Organization filters
  minTagCount?: number;
  
  // Quality filters
  popularOnly?: boolean; // Stories with above-average ratings
  hiddenGemsOnly?: boolean; // Unrated or low-rated stories
}

// Preset filter configurations
export interface FilterPreset {
  id: string;
  label: string;
  description?: string;
  filters: Partial<AdvancedFilters>;
  category: 'length' | 'date' | 'rating' | 'reading' | 'content' | 'organization';
}

// Library Statistics
export interface LibraryOverviewStats {
  // Collection Overview
  totalStories: number;
  totalAuthors: number;
  totalSeries: number;
  totalTags: number;
  totalCollections: number;
  uniqueSourceDomains: number;

  // Content Metrics
  totalWordCount: number;
  averageWordsPerStory: number;
  longestStory: StoryWordCount | null;
  shortestStory: StoryWordCount | null;

  // Reading Time
  totalReadingTimeMinutes: number;
  averageReadingTimeMinutes: number;
}

export interface StoryWordCount {
  id: string;
  title: string;
  authorName: string;
  wordCount: number;
  readingTimeMinutes: number;
}

// Top Tags Statistics
export interface TopTagsStats {
  topTags: TagStats[];
}

export interface TagStats {
  tagName: string;
  storyCount: number;
}

// Top Authors Statistics
export interface TopAuthorsStats {
  topAuthorsByStories: AuthorStats[];
  topAuthorsByWords: AuthorStats[];
}

export interface AuthorStats {
  authorId: string;
  authorName: string;
  storyCount: number;
  totalWords: number;
}

// Rating Statistics
export interface RatingStats {
  averageRating: number;
  totalRatedStories: number;
  totalUnratedStories: number;
  ratingDistribution: Record<number, number>; // rating -> count
}

// Source Domain Statistics
export interface SourceDomainStats {
  topDomains: DomainStats[];
  storiesWithSource: number;
  storiesWithoutSource: number;
}

export interface DomainStats {
  domain: string;
  storyCount: number;
}

// Reading Progress Statistics
export interface ReadingProgressStats {
  totalStories: number;
  readStories: number;
  unreadStories: number;
  percentageRead: number;
  totalWordsRead: number;
  totalWordsUnread: number;
}

// Reading Activity Statistics
export interface ReadingActivityStats {
  storiesReadLastWeek: number;
  wordsReadLastWeek: number;
  readingTimeMinutesLastWeek: number;
  dailyActivity: DailyActivity[];
}

export interface DailyActivity {
  date: string; // YYYY-MM-DD
  storiesRead: number;
  wordsRead: number;
}