<app-header></app-header>
<main class="container mx-auto px-4 py-6 lg:px-6 lg:py-8">
    <!-- Library Header -->
    <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between mb-6 lg:mb-8">
        <div>
            <h1 class="text-2xl lg:text-3xl font-bold" *ngIf="searchText">{{t('SEARCH_RESULT')}}</h1>
            <h1 class="text-2xl lg:text-3xl font-bold" *ngIf="!searchText">{{ categoryName || 'Category' }}</h1>
            <!-- <p class="text-wattpad-light mt-1" *ngIf="!searchText">Your saved stories and reading lists</p> -->
        </div>
        <!-- <div class="mt-4 lg:mt-0 flex space-x-3" *ngIf="!searchText">
            <button
                class="px-4 py-2 rounded-full font-medium bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors">
                <i class="fas fa-sliders-h mr-2"></i> Filter
            </button>
            <button
                class="px-4 py-2 rounded-full font-medium bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors">
                <i class="fas fa-sort mr-2"></i> Sort
            </button>
        </div> -->
    </div>




    <!-- Saved Stories Tab -->
    <div id="saved-tab" class="tab-content active">
        <div class="mb-6">


            <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 lg:gap-6"
                *ngIf="!isLoading && stories.length > 0">
                <!-- Story Card 1 -->
                <div class="story-card bg-white dark:bg-gray-800 rounded-lg shadow-sm overflow-hidden pointer"
                    *ngFor="let story of stories" (click)="goToSeries(story.id, story.story_title)">
                    <div class="relative">
                        <div class="text-center story-cover rounded-lg aspect-[4/6] bg-gray-200 dark:bg-gray-700 overflow-hidden">
                            <img *ngIf="story.story_image"
                                [src]="'https://infoways.in/funtelapi/' + story.story_image"
                                class="w-full  object-cover rounded mb-3">
                        </div>
                        

                    </div>
                    <div class="p-3">
                        <h3 class="font-medium line-clamp-1">{{ story.story_title }}</h3>
                        <p class="text-xs text-wattpad-light mt-1">By {{ story.author_name }} • {{
                            formatDate(story.story_created_at) }}.</p>
                        <div class="flex items-center mt-1 text-xs lg:text-sm text-wattpad-light">
  <i class="fas fa-star text-yellow-400 mr-1"></i>
  <span>{{ story.average_rating || 0 }}</span>
  <span class="mx-1">•</span>
  <span>{{ formatCount(story.views) }} {{t('READS')}}</span>
</div>
                    </div>
                </div>


            </div>
            <div class="text-wattpad-light mt-8 text-lg font-medium"
     *ngIf="!isLoading && stories.length === 0">
  <ng-container *ngIf="searchText">
    🔍 {{t('NO_SEARCH_RESULTS')}}.
  </ng-container>
  <ng-container *ngIf="!searchText">
    📁 {{t('NO_CONTENT_FOUND')}}.
  </ng-container>
</div>
        </div>




    </div>

    <!-- Reading Lists Tab -->
    <div id="reading-lists-tab" class="tab-content mb-6" *ngIf="relatedStories.length">
        <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between mb-6">
            <h2 class="text-lg lg:text-xl font-bold">{{t('RELATED_LISTS')}}</h2>

        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <div *ngFor="let story of relatedStories"
                class="bg-white dark:bg-gray-800 rounded-xl pointer shadow-sm overflow-hidden"
                (click)="goToSeries(story.id, story.story_title)">
                <div class="relative">
                    <div
                        class="aspect-[3/1] bg-gradient-to-r from-purple-500 to-pink-500 flex items-center justify-center">
                        <span class="text-white text-xl font-bold truncate px-2">{{ story.story_title }}</span>
                    </div>
                    <button
                        class="absolute top-2 right-2 w-8 h-8 rounded-full bg-black/50 flex items-center justify-center text-white hover:bg-wattpad-primary transition-colors">
                        <i class="fas fa-ellipsis-h"></i>
                    </button>
                </div>
                <div class="p-4">
                    <div class="flex justify-between items-center mb-3">
                        <h3 class="font-medium truncate">{{ story.story_title }}</h3>
                        <span class="text-xs bg-gray-100 dark:bg-gray-700 px-2 py-1 rounded-full">
                            {{ story.views || 0 }} {{t('VIEWS')}}
                        </span>
                    </div>
                    <p class="text-sm text-wattpad-light line-clamp-2">{{ story.description }}</p>
                    <p class="text-xs text-wattpad-light mt-1">By {{ story.author_name }} </p>
                </div>
            </div>
        </div>
    </div>

    <!-- Reading History Tab -->
    <div id="history-tab" class="tab-content" *ngIf="isLoggedIn && userProgress.length > 0">
        <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between mb-6">
            <h2 class="text-lg lg:text-xl font-bold">{{t('YOUR_READING_HISTORY')}}</h2>
            <button
                class="mt-2 lg:mt-0 px-4 py-2 rounded-full font-medium bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors pointer"
                (click)="clearHistory()">
                <i class="fas fa-trash-alt mr-2"></i> {{t('CLEAR_HISTORY')}}
            </button>
        </div>

        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm overflow-hidden">
            <div class="divide-y divide-gray-200 dark:divide-gray-700">
                <!-- History Item 1 -->
                <div class="p-4 flex items-start space-x-4"
                    *ngFor="let prog of userProgress.slice(0, visibleHistoryCount)" (click)="goToSeries(prog.storyId, prog.title)">
                    <div class="flex-shrink-0 w-12 h-16 bg-gray-200 dark:bg-gray-700 rounded overflow-hidden">
                        <img *ngIf="prog.image" [src]="prog.image" alt="Story Cover"
                            class="w-full h-full object-cover" />
                    </div>
                    <div class="flex-1">
                        <div class="flex justify-between">
                            <div>
                                <h3 class="font-medium">{{ prog.title }}</h3>
                                <p class="text-xs text-wattpad-light">By {{ prog.name }}</p>
                            </div>
                            <span class="text-xs text-wattpad-light">{{prog.timestamp}}</span>
                        </div>
                        <div class="mt-2">
                            <div class="flex justify-between text-xs text-wattpad-light mb-1">
                                <span>{{ prog.title }}</span>
                                <span>{{prog.percent}}%</span>
                            </div>
                            <div class="reading-progress w-full">
                                <div class="reading-progress-bar" style="width: 65%"></div>
                            </div>
                        </div>
                    </div>
                </div>


            </div>

            <div class="p-4 flex justify-center" *ngIf="userProgress.length > visibleHistoryCount">
                <button
                    class="px-4 py-2 rounded-full font-medium bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors pointer"
                    (click)="loadMoreHistory()">
                    {{t('LOAD_MORE')}}
                </button>
            </div>
        </div>
    </div>
</main>