---
import Layout from '@layouts/Layout.astro';
import Hero from '@components/preise/HeroSection.astro';
import Services from '@components/preise/ServicesSection.astro';
import Prices from '@components/preise/Price.astro';
import CtaSection from '@components/preise/CtaSection.astro';
import { authService } from '@lib/authService';
import { sliderService } from '@lib/sliderService';
import type { PriceService, Car } from '@/types/globalInterfaces';

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

// Получаем данные для секции услуг из PocketBase
const servicesResponse = await pb.collection('preise_services').getList(1, 50, {
    sort: 'order',
    fields: '*, features'
});

// Преобразуем RecordModel в PriceService
const services: PriceService[] = servicesResponse.items.map(item => ({
    id: item.id,
    title: item.title,
    description: item.description,
    icon: item.icon,
    features: Array.isArray(item.features) ? item.features : [],
    order: item.order,
    is_active: true,
    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: Car[] = sliderCars.map((car) => transformSliderDataToCar(car));

// Получаем данные для CTA секции из PocketBase
const ctaResponse = await pb.collection('preise_cta').getFirstListItem('is_active = true');

let ctaTitle: string = ctaResponse.title;
let ctaSubtitle: string = ctaResponse.subtitle;
let ctaButtonText: string = ctaResponse.btn_text;
let ctaHref: string = ctaResponse.href;

const servicesTitle = pageData.services_title || "Unsere Fahrzeuge für jeden Anlass";
---

<Layout {title} {description} {canonicalLink}>
    <h1 class="sr-only">Unsere Preise für Van- und Limousinenservices in Berlin</h1>

    <Hero />
    <div class="container mx-auto px-4 max-w-7xl">
        <Services {services} {servicesTitle} />
        <!-- Разделительная линия между верхним блоком с карточками и нижним блоком со слайдером -->
        <div class="my-12 flex items-center">
            <div class="grow border-t border-gray-200"></div>
            <div class="mx-4 text-gray-500">• • •</div>
            <div class="grow border-t border-gray-200"></div>
        </div>

        <!-- Слайдер & CTA -->
        <Prices {cars} />
        <CtaSection title={ctaTitle} subtitle={ctaSubtitle} buttonText={ctaButtonText} href={ctaHref} />
    </div>
</Layout>