---
import Layout from '@layouts/Layout.astro';
import HeroBlock from '@/components/about/HeroBlock.astro';
import StoryBlock from '@/components/about/StoryBlock.astro';
import ValuesBlock from '@/components/about/ValuesBlock.astro';
import FleetBlock from '@/components/about/FleetBlock.astro';
import CtaBlock from '@/components/about/CtaBlock.astro';
import SectionWrapper from '@/components/about/SectionWrapper.astro';
import { aboutPageLayout } from '@/components/about/aboutData';
import { authService } from '@/lib/authService';

// Создаем клиент PocketBase
const pb = authService.createClientFromRequest(Astro.request);

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

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

// Получаем данные для Hero блока из PocketBase
const heroDataResponse = await pb.collection('about_hero').getFirstListItem('is_active = true');
const heroData = {
    blockType: 'hero',
    title: heroDataResponse.title,
    subtitle: heroDataResponse.subtitle
};

// Получаем данные для Story блока из PocketBase
const storyDataResponse = await pb.collection('about_story').getFirstListItem('');
const storyData = {
    blockType: 'story',
    title: storyDataResponse.title,
    content: storyDataResponse.description,
    image: {
        url: `${import.meta.env.POCKETBASE_URL || import.meta.env.PUBLIC_POCKETBASE_URL || 'http://127.0.0.1:8090'}/api/files/${storyDataResponse.collectionName}/${storyDataResponse.id}/${storyDataResponse.image}`,
        alt: storyDataResponse.alt_img || storyDataResponse.title
    }
};

// Получаем данные для Values блока из PocketBase
const valuesDataResponse = await pb.collection('about_values').getFirstListItem('');
const valuesItemsResponse = await pb.collection('about_values_items').getList(1, 100, {
    sort: 'order',
    filter: 'is_active=true'
});

const valuesData = {
    blockType: 'values',
    title: valuesDataResponse.title,
    values: valuesItemsResponse.items.map(item => ({
        id: item.id,
        icon: item.icon,
        title: item.title,
        description: item.description
    }))
};

// Временные данные из aboutData.ts, которые будут заменены на данные из PocketBase
// const fleetData = aboutPageLayout.find(block => block.blockType === 'fleet');
const ctaData = aboutPageLayout.find(block => block.blockType === 'cta');
---

<Layout {title} {description} {canonicalLink}>
  <!-- hero -->
  <HeroBlock title={heroData.title} subtitle={heroData.subtitle} />

  <!-- team -->
  <SectionWrapper className="bg-gray-50 py-16 -mx-4">
    <StoryBlock
      title={storyData.title}
      content={storyData.content}
      image={storyData.image}
    />
  </SectionWrapper>

  <!--  features  -->
  <SectionWrapper className="bg-white py-16">
    <ValuesBlock
      title={valuesData.title}
      values={valuesData.values}
    />
  </SectionWrapper>

  <SectionWrapper className="bg-gray-50 py-16">
    <FleetBlock
      title="Unsere Flotte"
    />
  </SectionWrapper>

  <CtaBlock />
</Layout>