package com.storycove.service;

import com.storycove.entity.Collection;

/**
 * Special Collection subclass for search results that provides pre-calculated statistics
 * to avoid lazy loading issues when displaying collection lists.
 */
public class CollectionSearchResult extends Collection {
    
    private Integer storedStoryCount;
    private Integer storedTotalWordCount;
    private int wordsPerMinute = 200; // Default, can be overridden
    
    public CollectionSearchResult(Collection collection) {
        this.setId(collection.getId());
        this.setName(collection.getName());
        this.setDescription(collection.getDescription());
        this.setRating(collection.getRating());
        this.setIsArchived(collection.getIsArchived());
        this.setCreatedAt(collection.getCreatedAt());
        this.setUpdatedAt(collection.getUpdatedAt());
        this.setCoverImagePath(collection.getCoverImagePath());
        this.setTagNames(collection.getTagNames()); // Copy tag names for search results
        // Note: don't copy collectionStories or tags to avoid lazy loading issues
    }
    
    public void setStoredStoryCount(Integer storyCount) {
        this.storedStoryCount = storyCount;
    }
    
    public void setStoredTotalWordCount(Integer totalWordCount) {
        this.storedTotalWordCount = totalWordCount;
    }
    
    public void setWordsPerMinute(int wordsPerMinute) {
        this.wordsPerMinute = wordsPerMinute;
    }
    
    @Override
    public int getStoryCount() {
        return storedStoryCount != null ? storedStoryCount : 0;
    }
    
    @Override
    public int getTotalWordCount() {
        return storedTotalWordCount != null ? storedTotalWordCount : 0;
    }
    
    @Override
    public int getEstimatedReadingTime() {
        return Math.max(1, getTotalWordCount() / wordsPerMinute);
    }
    
    @Override
    public Double getAverageStoryRating() {
        // For search results, we don't calculate average rating to avoid complexity
        // This would require loading all stories. Can be enhanced later if needed.
        return null;
    }
}