---
import Layout from '@layouts/Layout.astro';
import ReviewsComponent from '@components/reviews/ReviewsWrapper.astro';
import { authService } from '@/lib/authService';
import NotificationToast from '@components/base/NotificationToast.astro';

// Получаем глобальные настройки сайта из PocketBase
const pb = authService.createClientFromRequest(Astro.request);
const siteSettings = await pb.collection('site_settings').getFirstListItem('');
const siteUrl = siteSettings.site_url;

// Получаем данные страницы из PocketBase
const pageData = await pb.collection('pages').getFirstListItem('slug = "bewertungen"');
const title = `${pageData.title} | ${siteSettings.site_name}`;
const description = pageData.meta_description;
const canonicalLink = pageData.canonical_url ? pageData.canonical_url : `${siteUrl}/${pageData.slug}`;

// 1. Логика пользователя
const user = Astro.locals.user;
const currentUser = user ? {
    id: user.id,
    name: user.name,
    email: user.email
} : null;

// 2. Логика уведомления
// Проверяем наличие флага loginSuccess И аутентификацию пользователя
const loginSuccess = Astro.url.searchParams.get('loginSuccess') === 'true' && !!user;

// 3. Загрузка отзывов (оставляем вашу логику как есть, сокращено для краткости)
let initialReviews = [];
try {
    const pb = authService.createClientFromRequest(Astro.request);
    let reviews;
    try {
        reviews = await pb.collection('reviews').getFullList({ sort: '-created', expand: 'author' });
    } catch (accessError) {
        reviews = await pb.collection('reviews').getFullList({ sort: '-created' });
    }
    initialReviews = reviews.map((review) => ({
        id: review.id,
        title: review.title,
        content: review.content,
        rating: review.rating,
        author: review.expand?.author?.name || review.expand?.author?.email || 'Anonym',
        createdAt: review.created
    }));
} catch (error) {
    console.error('Reviews load error:', error);
}
---

<Layout
        title={title}
        description={description}
        canonicalLink={canonicalLink}
        session={user ? {user: user} : null}
        status={user ? 'authenticated' : 'unauthenticated'}
>
    <ReviewsComponent  />
</Layout>