<?php

declare(strict_types=1);

header('Content-Type: text/html; charset=UTF-8');

require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'app.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_session.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_member_auth.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_masseur_favorites.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_masseur_image.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_template.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_ui_settings.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_page_counter.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_public_list_query.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_admin_nav_visibility.php';

require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_search_probe.php';

fate_public_session_start();
$fatePublicMember = fate_public_member_current();

$targetMemberId = isset($_GET['member']) ? (int) $_GET['member'] : 0;
$pdo = null;
$dbErr = '';
try {
    require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'pdo.php';
    if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO) {
        $pdo = $GLOBALS['pdo'];
    }
} catch (Throwable $e) {
    $dbErr = $e->getMessage();
}

$publicData = ['ok' => false, 'error' => '此會員的收藏清單未公開。', 'member' => null, 'items' => []];
if ($pdo instanceof PDO && $targetMemberId > 0) {
    $publicData = fate_masseur_favorites_public_for_member($pdo, $targetMemberId);
}

$memberInfo = is_array($publicData['member'] ?? null) ? $publicData['member'] : null;
$publicFavorites = is_array($publicData['items'] ?? null) ? $publicData['items'] : [];
$publicFavoritesOk = !empty($publicData['ok']);
$publicFavoritesErr = (string) ($publicData['error'] ?? '');

$publicDisp = $memberInfo !== null ? (string) ($memberInfo['display_name'] ?? '會員') : '會員';
$publicFavGenders = fate_masseur_gender_labels();

$fateAdminNavVisible = fate_admin_nav_visible_for_frontend();
$cityFilter = isset($_GET['city']) ? trim((string) $_GET['city']) : '';
if (strlen($cityFilter) > 120) {
    $cityFilter = substr($cityFilter, 0, 120);
}
$sort = isset($_GET['sort']) ? strtolower(trim((string) $_GET['sort'])) : 'rating';
if (!in_array($sort, ['rating', 'name', 'recent'], true)) {
    $sort = 'rating';
}
$storeSearchQ = fate_search_normalize_public_query(isset($_GET['q']) ? (string) $_GET['q'] : '');
$buildHomeQuery = static function (array $overrides): string {
    return fate_build_home_list_query($_GET, $overrides);
};
$storeSearchTotal = null;

$fateBrandForTitle = fate_public_site_branding();
$fatePageTitle = ($publicFavoritesOk ? ($publicDisp . ' 的收藏技師') : '收藏清單') . ' — ' . $fateBrandForTitle['title_main'];
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'fate_seo.php';
$fateSeoContext = $publicFavoritesOk
    ? [
        'description' => fate_seo_trim_description($publicDisp . ' 公開分享的收藏技師清單，共 ' . count($publicFavorites) . ' 位。'),
        'canonical' => fate_absolute_url('member_favorites_public.php?member=' . $targetMemberId),
    ]
    : fate_seo_private_page_context();
$fateMemberIdForLog = fate_page_stats_member_id($fatePublicMember);
$fatePageStatsFooter = fate_page_stats_for_footer($pdo, 'member.favorites_public.' . $targetMemberId, $fateMemberIdForLog, $fatePageTitle);
$fatePageHitCount = $fatePageStatsFooter['fatePageHitCount'];
$fatePageStatsDbOk = $fatePageStatsFooter['fatePageStatsDbOk'];

$fateSkipLayoutTopbar = fate_is_minimal_theme();

if (fate_is_minimal_theme()) {
    $fateMinimalIncludePageStyles = true;
    require __DIR__ . '/templates/minimal/partials/head.php';
    $fateSkipLayoutTopbar = true;
    echo '<body class="minimal-body minimal-body--inner">';
    require __DIR__ . '/templates/minimal/partials/bar.php';
    require __DIR__ . '/templates/default/member_favorites_public_main.php';
    require __DIR__ . '/templates/minimal/partials/footer.php';
} else {
    require __DIR__ . '/templates/default/partials/head.php';
    require __DIR__ . '/templates/default/member_favorites_public_main.php';
    require __DIR__ . '/templates/default/partials/footer.php';
}
