<?php
 
 
 
 
 
 
 
 
 
 
 
 
 
 

$activeTab = $activeTab ?? 'favorites';
$joinDate = !empty($profileUser['created_at']) ? date('M Y', strtotime($profileUser['created_at'])) : '';
$initial = strtoupper(mb_substr($profileUser['username'] ?? 'U', 0, 1));
$displayName = htmlspecialchars(ucfirst($profileUser['username'] ?? ''));
?>

<div class="profile-page">

    
    <header class="profile-hero">
        <div class="profile-hero__bg" aria-hidden="true"></div>
        <div class="profile-hero__main">
            <div class="profile-hero__avatar"><?php echo $initial; ?></div>
            <div class="profile-hero__identity">
                <h1 class="profile-hero__name"><?php echo $displayName; ?></h1>
                <?php if ($joinDate): ?>
                    <p class="profile-hero__meta">
                        <i class="fa-regular fa-calendar"></i>
                        <?php echo __('member_since', [':date' => $joinDate]); ?>
                    </p>
                <?php endif; ?>
            </div>
        </div>
        <div class="profile-hero__stats">
            <div class="profile-hero__stat">
                <span class="profile-hero__stat-num"><?php echo Format::number($favCount); ?></span>
                <span class="profile-hero__stat-label"><?php echo __('my_favorites'); ?></span>
            </div>
            <div class="profile-hero__stat">
                <span class="profile-hero__stat-num"><?php echo Format::number($likedCount); ?></span>
                <span class="profile-hero__stat-label"><?php echo __('liked_videos'); ?></span>
            </div>
            <div class="profile-hero__stat">
                <span class="profile-hero__stat-num"><?php echo Format::number($histCount); ?></span>
                <span class="profile-hero__stat-label"><?php echo __('watch_history'); ?></span>
            </div>
            <div class="profile-hero__stat">
                <span class="profile-hero__stat-num"><?php echo Format::number($commentCount); ?></span>
                <span class="profile-hero__stat-label"><?php echo __('comments'); ?></span>
            </div>
        </div>
    </header>

    
    <nav class="profile-nav">
        <a href="<?php echo url('/profile'); ?>?tab=favorites"
           class="profile-nav__tab<?php echo $activeTab === 'favorites' ? ' active' : ''; ?>">
            <i class="fa-solid fa-heart"></i>
            <span class="profile-nav__label"><?php echo __('my_favorites'); ?></span>
            <?php if ($favCount > 0): ?>
                <span class="profile-nav__badge"><?php echo Format::number($favCount); ?></span>
            <?php endif; ?>
        </a>
        <a href="<?php echo url('/profile'); ?>?tab=liked"
           class="profile-nav__tab<?php echo $activeTab === 'liked' ? ' active' : ''; ?>">
            <i class="fa-solid fa-thumbs-up"></i>
            <span class="profile-nav__label"><?php echo __('liked_videos'); ?></span>
            <?php if ($likedCount > 0): ?>
                <span class="profile-nav__badge"><?php echo Format::number($likedCount); ?></span>
            <?php endif; ?>
        </a>
        <a href="<?php echo url('/profile'); ?>?tab=history"
           class="profile-nav__tab<?php echo $activeTab === 'history' ? ' active' : ''; ?>">
            <i class="fa-solid fa-clock-rotate-left"></i>
            <span class="profile-nav__label"><?php echo __('watch_history'); ?></span>
            <?php if ($histCount > 0): ?>
                <span class="profile-nav__badge"><?php echo Format::number($histCount); ?></span>
            <?php endif; ?>
        </a>
        <a href="<?php echo url('/profile'); ?>?tab=account"
           class="profile-nav__tab<?php echo $activeTab === 'account' ? ' active' : ''; ?>">
            <i class="fa-solid fa-gear"></i>
            <span class="profile-nav__label"><?php echo __('account_settings'); ?></span>
        </a>
    </nav>

    
    <div class="profile-body">

        <?php if ($activeTab === 'account'): ?>
        
        <div class="profile-settings">

            <?php if (!empty($error)): ?>
                <div class="profile-alert profile-alert--error">
                    <i class="fa-solid fa-circle-exclamation"></i>
                    <span><?php echo htmlspecialchars($error); ?></span>
                </div>
            <?php endif; ?>
            <?php if (!empty($success)): ?>
                <div class="profile-alert profile-alert--success">
                    <i class="fa-solid fa-circle-check"></i>
                    <span><?php echo htmlspecialchars($success); ?></span>
                </div>
            <?php endif; ?>

            <form action="<?php echo url('/profile'); ?>" method="POST" class="profile-form">
                <?php echo Router::csrfField(); ?>

                
                <div class="profile-form__section">
                    <h2 class="profile-form__title">
                        <i class="fa-solid fa-user"></i>
                        <?php echo __('personal_info'); ?>
                    </h2>
                    <div class="profile-form__grid">
                        <div class="profile-form__field">
                            <label for="username"><?php echo __('username'); ?></label>
                            <div class="profile-form__input-wrap">
                                <i class="fa-solid fa-at"></i>
                                <input type="text" id="username" name="username" required
                                       value="<?php echo htmlspecialchars($profileUser['username'] ?? ''); ?>"
                                       placeholder="<?php echo __('username'); ?>">
                            </div>
                        </div>
                        <div class="profile-form__field">
                            <label for="email"><?php echo __('email'); ?></label>
                            <div class="profile-form__input-wrap">
                                <i class="fa-solid fa-envelope"></i>
                                <input type="email" id="email" name="email" required
                                       value="<?php echo htmlspecialchars($profileUser['email'] ?? ''); ?>"
                                       placeholder="<?php echo __('email'); ?>">
                            </div>
                        </div>
                    </div>
                </div>

                
                <div class="profile-form__section">
                    <h2 class="profile-form__title">
                        <i class="fa-solid fa-shield-halved"></i>
                        <?php echo __('security'); ?>
                    </h2>
                    <p class="profile-form__hint"><?php echo __('new_password_hint'); ?></p>
                    <div class="profile-form__field" style="margin-bottom: 16px;">
                        <label for="current_password"><?php echo __('current_password'); ?></label>
                        <div class="profile-form__input-wrap">
                            <i class="fa-solid fa-key"></i>
                            <input type="password" id="current_password" name="current_password" autocomplete="current-password"
                                   placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;">
                        </div>
                    </div>
                    <div class="profile-form__grid">
                        <div class="profile-form__field">
                            <label for="password"><?php echo __('new_password'); ?></label>
                            <div class="profile-form__input-wrap">
                                <i class="fa-solid fa-lock"></i>
                                <input type="password" id="password" name="password" autocomplete="new-password"
                                       placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;">
                            </div>
                        </div>
                        <div class="profile-form__field">
                            <label for="password_confirm"><?php echo __('confirm_new_password'); ?></label>
                            <div class="profile-form__input-wrap">
                                <i class="fa-solid fa-lock"></i>
                                <input type="password" id="password_confirm" name="password_confirm" autocomplete="new-password"
                                       placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="profile-form__actions">
                    <button type="submit" class="btn btn-primary profile-form__submit">
                        <i class="fa-solid fa-check"></i>
                        <?php echo __('update_profile'); ?>
                    </button>
                </div>
            </form>
        </div>

        <?php elseif ($activeTab === 'favorites'): ?>
        
        <?php if (!empty($albumsEnabled)): ?>
            <?php echo simplyProfileTypePills('favorites', $contentType ?? 'video', (int) $favCount, (int) $albumFavCount); ?>
        <?php endif; ?>
        <?php if (($contentType ?? 'video') === 'album'): ?>
            <?php if (!empty($albumItems)): ?>
                <div class="profile-section-header">
                    <p class="profile-section-header__count">
                        <?php echo $albumPagination->total !== 1
                            ? __('albums_in_favorites_plural', [':count' => Format::number($albumPagination->total)])
                            : __('albums_in_favorites', [':count' => Format::number($albumPagination->total)]); ?>
                    </p>
                </div>
                <div class="album-grid">
                    <?php foreach ($albumItems as $album): ?><?php echo simplyAlbumCard($album); ?><?php endforeach; ?>
                </div>
                <?php echo simplyPagination($albumPagination, url('/profile') . '?tab=favorites&type=album'); ?>
            <?php else: ?>
                <div class="profile-empty">
                    <div class="profile-empty__icon"><i class="fa-light fa-heart"></i></div>
                    <h3 class="profile-empty__title"><?php echo __('no_favorite_albums'); ?></h3>
                    <p class="profile-empty__text"><?php echo __('no_favorite_albums_desc'); ?></p>
                    <a href="<?php echo url('/albums'); ?>" class="btn btn-primary profile-empty__btn">
                        <i class="fa-solid fa-images"></i>
                        <?php echo __('discover_albums'); ?>
                    </a>
                </div>
            <?php endif; ?>
        <?php elseif (!empty($favorites)): ?>
            <div class="profile-section-header">
                <p class="profile-section-header__count">
                    <?php echo $favPagination->total !== 1
                        ? __('videos_in_favorites_plural', [':count' => Format::number($favPagination->total)])
                        : __('videos_in_favorites', [':count' => Format::number($favPagination->total)]); ?>
                </p>
            </div>
            <div class="video-grid">
                <?php foreach ($favorites as $video): ?>
                    <?php echo simplyVideoCard($video); ?>
                <?php endforeach; ?>
            </div>
            <?php echo simplyPagination($favPagination, url('/profile') . '?tab=favorites'); ?>
        <?php else: ?>
            <div class="profile-empty">
                <div class="profile-empty__icon">
                    <i class="fa-light fa-heart"></i>
                </div>
                <h3 class="profile-empty__title"><?php echo __('no_favorites'); ?></h3>
                <p class="profile-empty__text"><?php echo __('no_favorites_desc') ?: 'Videos you favorite will appear here.'; ?></p>
                <a href="<?php echo url('/'); ?>" class="btn btn-primary profile-empty__btn">
                    <i class="fa-solid fa-play"></i>
                    <?php echo __('discover_videos'); ?>
                </a>
            </div>
        <?php endif; ?>

        <?php elseif ($activeTab === 'liked'): ?>
        
        <?php if (!empty($albumsEnabled)): ?>
            <?php echo simplyProfileTypePills('liked', $contentType ?? 'video', (int) $likedCount, (int) $albumLikedCount); ?>
        <?php endif; ?>
        <?php if (($contentType ?? 'video') === 'album'): ?>
            <?php if (!empty($albumItems)): ?>
                <div class="profile-section-header">
                    <p class="profile-section-header__count">
                        <?php echo $albumPagination->total !== 1
                            ? __('liked_albums_count_plural', [':count' => Format::number($albumPagination->total)])
                            : __('liked_albums_count', [':count' => Format::number($albumPagination->total)]); ?>
                    </p>
                </div>
                <div class="album-grid">
                    <?php foreach ($albumItems as $album): ?><?php echo simplyAlbumCard($album); ?><?php endforeach; ?>
                </div>
                <?php echo simplyPagination($albumPagination, url('/profile') . '?tab=liked&type=album'); ?>
            <?php else: ?>
                <div class="profile-empty">
                    <div class="profile-empty__icon"><i class="fa-light fa-thumbs-up"></i></div>
                    <h3 class="profile-empty__title"><?php echo __('no_liked_albums'); ?></h3>
                    <p class="profile-empty__text"><?php echo __('no_liked_albums_desc'); ?></p>
                    <a href="<?php echo url('/albums'); ?>" class="btn btn-primary profile-empty__btn">
                        <i class="fa-solid fa-images"></i>
                        <?php echo __('discover_albums'); ?>
                    </a>
                </div>
            <?php endif; ?>
        <?php elseif (!empty($liked)): ?>
            <div class="profile-section-header">
                <p class="profile-section-header__count">
                    <?php echo $likedPagination->total !== 1
                        ? __('liked_videos_count_plural', [':count' => Format::number($likedPagination->total)])
                        : __('liked_videos_count', [':count' => Format::number($likedPagination->total)]); ?>
                </p>
            </div>
            <div class="video-grid">
                <?php foreach ($liked as $video): ?>
                    <?php echo simplyVideoCard($video); ?>
                <?php endforeach; ?>
            </div>
            <?php echo simplyPagination($likedPagination, url('/profile') . '?tab=liked'); ?>
        <?php else: ?>
            <div class="profile-empty">
                <div class="profile-empty__icon">
                    <i class="fa-light fa-thumbs-up"></i>
                </div>
                <h3 class="profile-empty__title"><?php echo __('no_liked_videos'); ?></h3>
                <p class="profile-empty__text"><?php echo __('no_liked_desc') ?: 'Videos you like will appear here.'; ?></p>
                <a href="<?php echo url('/'); ?>" class="btn btn-primary profile-empty__btn">
                    <i class="fa-solid fa-play"></i>
                    <?php echo __('discover_videos'); ?>
                </a>
            </div>
        <?php endif; ?>

        <?php elseif ($activeTab === 'history'): ?>
        
        <?php if (!empty($history)): ?>
            <div class="profile-section-header">
                <p class="profile-section-header__count">
                    <?php echo $histPagination->total !== 1
                        ? __('videos_watched_plural', [':count' => Format::number($histPagination->total)])
                        : __('videos_watched', [':count' => Format::number($histPagination->total)]); ?>
                </p>
                <form action="<?php echo url('/history/clear'); ?>" method="POST"
                      onsubmit="return confirm('<?php echo __('clear_history_confirm'); ?>');">
                    <?php echo Router::csrfField(); ?>
                    <button type="submit" class="btn btn-sm profile-section-header__clear">
                        <i class="fa-solid fa-trash-can"></i>
                        <?php echo __('clear_history'); ?>
                    </button>
                </form>
            </div>
            <div class="video-grid">
                <?php foreach ($history as $video): ?>
                    <?php echo simplyVideoCard($video); ?>
                <?php endforeach; ?>
            </div>
            <?php echo simplyPagination($histPagination, url('/profile') . '?tab=history'); ?>
        <?php else: ?>
            <div class="profile-empty">
                <div class="profile-empty__icon">
                    <i class="fa-light fa-clock-rotate-left"></i>
                </div>
                <h3 class="profile-empty__title"><?php echo __('history_empty'); ?></h3>
                <p class="profile-empty__text"><?php echo __('history_empty_desc') ?: 'Videos you watch will appear here.'; ?></p>
                <a href="<?php echo url('/'); ?>" class="btn btn-primary profile-empty__btn">
                    <i class="fa-solid fa-play"></i>
                    <?php echo __('discover_videos'); ?>
                </a>
            </div>
        <?php endif; ?>
        <?php endif; ?>

    </div>
</div>
