<?php

declare(strict_types=1);

/**
 * 會員頁 — 收藏文章（論壇回文 + 技師評論）。
 *
 * @var list<array<string,mixed>> $memberSavedThreads
 * @var list<array<string,mixed>> $memberSavedReviews
 * @var int $memberSavedArticlesCount
 * @var bool $hasSavedForumPosts
 * @var bool $hasSavedMasseurReviews
 * @var string $csrf
 */

require_once dirname(__DIR__, 3) . '/includes/fate_post_favorites.php';
require_once dirname(__DIR__, 3) . '/includes/fate_masseur_review_engagement.php';

$hasAnySaved = ($memberSavedThreads ?? []) !== [] || ($memberSavedReviews ?? []) !== [];

?>
<div class="member-profile-tab-panel__inner">
    <div class="member-fav-head">
        <div>
            <h3 class="member-profile-tab-subtitle">收藏文章</h3>
            <p class="sub member-profile-hint">
                <?php if (!empty($hasSavedForumPosts) && !empty($hasSavedMasseurReviews)): ?>
                    您收藏的<strong>討論區回文</strong>與<strong>技師評論</strong>；在文章下方點「☆ 收藏」即可加入。
                <?php elseif (!empty($hasSavedForumPosts)): ?>
                    您收藏的<strong>討論區回文</strong>；在討論串中每一則文章下方點「☆ 收藏」即可加入。
                <?php else: ?>
                    您收藏的<strong>技師評論</strong>；在店家頁技師評論下方點「☆ 收藏」即可加入。
                <?php endif; ?>
            </p>
        </div>
    </div>

    <?php if (!$hasAnySaved): ?>
        <p class="member-profile-empty">
            尚無收藏的文章。
            <?php if (!empty($hasSavedForumPosts)): ?>
                至<a href="forum/index.php" class="text-accent-link">討論區</a>在回文下方點選「☆ 收藏」即可加入。
            <?php endif; ?>
            <?php if (!empty($hasSavedMasseurReviews)): ?>
                <?php if (!empty($hasSavedForumPosts)): ?><br><?php endif; ?>
                或至<a href="index.php" class="text-accent-link">店家探索</a>在技師評論下方點選「☆ 收藏」。
            <?php endif; ?>
        </p>
    <?php else: ?>
        <?php if (($memberSavedReviews ?? []) !== []): ?>
            <?php if (($memberSavedThreads ?? []) !== []): ?>
                <h4 class="member-saved-section-title">技師評論</h4>
            <?php endif; ?>
            <ul class="member-thread-fav-list">
                <?php foreach ($memberSavedReviews as $item): ?>
                    <?php
                    $reviewId = (int) ($item['review_id'] ?? 0);
                    $contextTitle = (string) ($item['context_title'] ?? '技師');
                    $typeLabel = (string) ($item['type_label'] ?? '技師評論');
                    $reviewUrl = (string) ($item['review_url'] ?? 'index.php');
                    $favDate = substr((string) ($item['favorited_at'] ?? ''), 0, 10);
                    $authorName = (string) ($item['author_name'] ?? '會員');
                    $excerpt = (string) ($item['excerpt'] ?? '');
                    $isOrphan = (int) ($item['is_orphan'] ?? 0) === 1;
                    ?>
                    <li class="member-thread-fav-card">
                        <div class="member-thread-fav-card__main">
                            <p class="member-thread-fav-card__type">
                                <?= htmlspecialchars($typeLabel, ENT_QUOTES, 'UTF-8') ?>
                                <?php if ($isOrphan): ?> · <span class="member-thread-fav-card__orphan">來源已隱藏</span><?php endif; ?>
                            </p>
                            <p class="member-thread-fav-card__thread">
                                <span class="member-thread-fav-card__thread-label">技師</span>
                                <?= htmlspecialchars($contextTitle, ENT_QUOTES, 'UTF-8') ?>
                            </p>
                            <p class="member-thread-fav-card__excerpt"><?= htmlspecialchars($excerpt, ENT_QUOTES, 'UTF-8') ?></p>
                            <p class="member-thread-fav-card__meta">
                                <?= htmlspecialchars($authorName, ENT_QUOTES, 'UTF-8') ?>
                                <?php if ($favDate !== ''): ?> · 收藏 <?= htmlspecialchars($favDate, ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
                            </p>
                        </div>
                        <div class="member-thread-fav-card__side">
                            <a class="member-profile-btn member-profile-btn--sm" href="<?= htmlspecialchars($reviewUrl, ENT_QUOTES, 'UTF-8') ?>">前往評論</a>
                            <form method="post" action="member_profile.php?tab=saved_threads" class="member-thread-fav-unfav-form">
                                <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf, ENT_QUOTES, 'UTF-8') ?>">
                                <input type="hidden" name="action" value="review_favorite_toggle">
                                <input type="hidden" name="review_id" value="<?= $reviewId ?>">
                                <button type="submit" class="member-thread-fav-unfav-btn" title="取消收藏">★ 取消收藏</button>
                            </form>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>

        <?php if (($memberSavedThreads ?? []) !== []): ?>
            <?php if (($memberSavedReviews ?? []) !== []): ?>
                <h4 class="member-saved-section-title">討論區回文</h4>
            <?php endif; ?>
            <ul class="member-thread-fav-list">
                <?php foreach ($memberSavedThreads as $item): ?>
                    <?php
                    $postId = (int) ($item['post_id'] ?? 0);
                    $threadTitle = trim((string) ($item['title'] ?? ''));
                    if ($threadTitle === '') {
                        $threadTitle = '（無標題）';
                    }
                    $typeLabel = (string) ($item['type_label'] ?? '討論');
                    $postUrl = (string) ($item['post_url'] ?? 'forum/index.php');
                    $favDate = substr((string) ($item['favorited_at'] ?? ''), 0, 10);
                    $floorNo = (int) ($item['floor_no'] ?? 0);
                    $authorName = (string) ($item['author_name'] ?? '會員');
                    $excerpt = (string) ($item['excerpt'] ?? '');
                    $isOrphan = (int) ($item['is_orphan'] ?? 0) === 1;
                    ?>
                    <li class="member-thread-fav-card">
                        <div class="member-thread-fav-card__main">
                            <p class="member-thread-fav-card__type">
                                <?= htmlspecialchars($typeLabel, ENT_QUOTES, 'UTF-8') ?>
                                <?php if ($floorNo > 0): ?> · #<?= $floorNo ?><?php endif; ?>
                                <?php if ($isOrphan): ?> · <span class="member-thread-fav-card__orphan">來源已移除</span><?php endif; ?>
                            </p>
                            <p class="member-thread-fav-card__thread">
                                <span class="member-thread-fav-card__thread-label">主題</span>
                                <?= htmlspecialchars($threadTitle, ENT_QUOTES, 'UTF-8') ?>
                            </p>
                            <p class="member-thread-fav-card__excerpt"><?= htmlspecialchars($excerpt, ENT_QUOTES, 'UTF-8') ?></p>
                            <p class="member-thread-fav-card__meta">
                                <?= htmlspecialchars($authorName, ENT_QUOTES, 'UTF-8') ?>
                                <?php if ($favDate !== ''): ?> · 收藏 <?= htmlspecialchars($favDate, ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
                            </p>
                        </div>
                        <div class="member-thread-fav-card__side">
                            <a class="member-profile-btn member-profile-btn--sm" href="<?= htmlspecialchars($postUrl, ENT_QUOTES, 'UTF-8') ?>">前往該樓</a>
                            <?php fate_post_favorite_render_member_unfav($postId, $csrf); ?>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    <?php endif; ?>
</div>

