---
import Layout from '@layouts/Layout.astro';
import Hero  from "@components/home/hero/Hero.astro";
import HowItWorks from "@components/home/howIt/HowItWorks.astro";
import CarsSlider from "@components/home/slider/CarsSlider";
import Faq from "@components/faq/FaqHomepage";
import LatestReviews from "@components/reviews/LatestReviews.astro";
import { authService } from '@/lib/authService';
import { sliderService } from '@/lib/sliderService';

// Получаем глобальные настройки сайта из 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 = "home"');
const title = `${pageData.title} | ${pageData.meta_title}`;
const description = pageData.meta_description;
const canonicalLink = pageData.canonical_url ? pageData.canonical_url : `${siteUrl}/`;

// Получаем FAQ данные для главной страницы из PocketBase
const faqItemsResponse = await pb.collection('faq_items').getList(1, 6, {
    sort: 'order',
    filter: 'is_active=true && show_on_homepage=true',
    fields: 'question,answer,order,show_on_homepage,is_active,category,created,updated,collectionId,collectionName,id'
});

// Преобразуем RecordModel в FaqItem
const faqItems = faqItemsResponse.items.map(item => ({
    id: item.id,
    question: item.question,
    answer: item.answer,
    show_on_homepage: item.show_on_homepage,
    order: item.order,
    is_active: item.is_active,
    category: item.category,
    collectionId: item.collectionId,
    collectionName: item.collectionName,
    created: item.created,
    updated: item.updated
}));

// Получаем данные для слайдера автомобилей из PocketBase
const sliderCars = await sliderService.getSliderCars(pb);

// Преобразуем данные в формат, совместимый с Car
const { transformSliderDataToCar } = await import('@/components/home/slider/SliderContent');
const cars = sliderCars.map((car) => transformSliderDataToCar(car));
---

<Layout {title} {description} {canonicalLink}>
	<Hero />
	<HowItWorks />
	<CarsSlider cars={cars} client:visible />
    <LatestReviews />
	<Faq faqItems={faqItems} client:visible />
</Layout>
 