<?php

declare(strict_types=1);

/**
 * 公開收藏清單主內容。
 *
 * @var bool $publicFavoritesOk
 * @var string $publicFavoritesErr
 * @var string $publicDisp
 * @var list<array<string,mixed>> $publicFavorites
 * @var array<string,string> $publicFavGenders
 * @var string $dbErr
 * @var int $targetMemberId
 */

$fateSkipLayoutTopbar = !empty($fateSkipLayoutTopbar);
if (!$fateSkipLayoutTopbar):
?>
<body>
    <header class="topbar">
        <div class="topbar-inner">
            <a class="brand" href="./"><?= htmlspecialchars($fateBrand['title_main'] ?? 'FATE', ENT_QUOTES, 'UTF-8') ?> <span><?= htmlspecialchars($fateBrand['title_sub'] ?? '店家探索', ENT_QUOTES, 'UTF-8') ?></span></a>
            <nav>
                <a href="./">探索列表</a>
                <?php if ($fatePublicMember !== null): ?>
                    <a href="member_profile.php">我的帳號</a>
                    <a href="logout.php">登出</a>
                <?php else: ?>
                    <a href="login.php">登入</a>
                <?php endif; ?>
                <?php if (!empty($fateAdminNavVisible)): ?>
                    <a href="admin/">管理後台</a>
                <?php endif; ?>
            </nav>
        </div>
    </header>
<?php else: ?>
<div class="minimal-page">
<?php endif; ?>

    <div class="layout-shell layout-shell--detail">
        <div class="layout">
            <div class="layout-main">
                <nav class="store-bc" aria-label="麵包屑">
                    <a href="./">探索</a>
                    <span class="store-bc__sep" aria-hidden="true">/</span>
                    <span class="store-bc__cur">公開收藏</span>
                </nav>

                <header class="page-head page-head--compact">
                    <h1 class="page-title"><?= htmlspecialchars($publicDisp, ENT_QUOTES, 'UTF-8') ?> 的收藏技師</h1>
                    <p class="sub">會員自行公開的收藏清單，僅顯示技師與體驗狀態，不含私人評論內容。</p>
                </header>

                <?php if ($dbErr !== ''): ?>
                    <div class="member-profile-msg member-profile-msg--bad">資料庫：<?= htmlspecialchars($dbErr, ENT_QUOTES, 'UTF-8') ?></div>
                <?php endif; ?>

                <?php if (!$publicFavoritesOk): ?>
                    <div class="member-profile-msg member-profile-msg--warn"><?= htmlspecialchars($publicFavoritesErr, ENT_QUOTES, 'UTF-8') ?></div>
                <?php elseif ($publicFavorites === []): ?>
                    <p class="member-profile-empty">此會員目前沒有公開的收藏技師。</p>
                <?php else: ?>
                    <section class="store-card-fate store-card-fate--block member-profile-card" aria-label="收藏清單">
                        <ul class="member-fav-list member-fav-list--public">
                            <?php foreach ($publicFavorites as $fav): ?>
                                <?php
                                $mid = (int) ($fav['masseur_id'] ?? 0);
                                $mName = (string) ($fav['name'] ?? '');
                                $mGender = (string) ($fav['gender'] ?? 'female');
                                $mGenderLabel = $publicFavGenders[$mGender] ?? '女';
                                $mPhoto = fate_masseur_resolve_photo_public_url((string) ($fav['photo_rel'] ?? ''), $mGender);
                                $mHasPhoto = $mPhoto !== null;
                                $storeId = (int) ($fav['spa_store_id'] ?? 0);
                                $storeName = trim((string) ($fav['store_name'] ?? ''));
                                $cityName = trim((string) ($fav['city_name'] ?? ''));
                                $districtName = trim((string) ($fav['district_name'] ?? ''));
                                $loc = $cityName;
                                if ($districtName !== '') {
                                    $loc = ($loc !== '' ? $loc . ' ' : '') . $districtName;
                                }
                                $favDate = substr((string) ($fav['favorited_at'] ?? ''), 0, 10);
                                $isExperienced = (int) ($fav['is_experienced'] ?? 0) === 1;
                                $expDate = substr((string) ($fav['experienced_at'] ?? ''), 0, 10);
                                ?>
                                <li class="member-fav-card member-fav-card--public">
                                    <div class="member-fav-card__top">
                                        <div class="member-fav-card__avatar member-fav-card__avatar--<?= htmlspecialchars($mGender, ENT_QUOTES, 'UTF-8') ?>">
                                            <?php if ($mHasPhoto): ?>
                                                <img src="<?= htmlspecialchars($mPhoto, ENT_QUOTES, 'UTF-8') ?>" alt="" loading="lazy" decoding="async">
                                            <?php else: ?>
                                                <span aria-hidden="true"><?= $mGender === 'male' ? '♂' : ($mGender === 'female' ? '♀' : '☻') ?></span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="member-fav-card__body">
                                            <p class="member-fav-card__name">
                                                <?php if ($storeId > 0): ?>
                                                    <a href="store.php?id=<?= $storeId ?>#masseur-<?= $mid ?>" class="text-accent-link"><?= htmlspecialchars($mName, ENT_QUOTES, 'UTF-8') ?></a>
                                                <?php else: ?>
                                                    <?= htmlspecialchars($mName, ENT_QUOTES, 'UTF-8') ?>
                                                <?php endif; ?>
                                                <span class="masseur-card__gender"><?= htmlspecialchars($mGenderLabel, ENT_QUOTES, 'UTF-8') ?></span>
                                            </p>
                                            <?php if ($storeName !== ''): ?>
                                                <p class="member-fav-card__store">
                                                    <?= htmlspecialchars($storeName, ENT_QUOTES, 'UTF-8') ?>
                                                    <?php if ($loc !== ''): ?> · <?= htmlspecialchars($loc, ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
                                                </p>
                                            <?php endif; ?>
                                            <p class="member-fav-card__meta">
                                                收藏 <?= htmlspecialchars($favDate, ENT_QUOTES, 'UTF-8') ?>
                                                <?php if ($isExperienced): ?>
                                                    · 已體驗<?= $expDate !== '' ? (' ' . htmlspecialchars($expDate, ENT_QUOTES, 'UTF-8')) : '' ?>
                                                <?php else: ?>
                                                    · 未體驗
                                                <?php endif; ?>
                                            </p>
                                        </div>
                                    </div>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </section>
                <?php endif; ?>
            </div>

            <?php require __DIR__ . '/partials/sidebar.php'; ?>
        </div>
    </div>
<?php if ($fateSkipLayoutTopbar): ?>
</div>
<?php endif; ?>
