---
import { COLOR_CLASSES } from '@constants/colors';
import { authService } from '@/lib/authService';
import { Step } from '@/types/globalInterfaces';

// 1. Словарь иконок
// Ключи (FaPencilAlt, FaChild и т.д.) должны совпадать с полем "icon" в JSON вашего PocketBase
const modernIcons = {
    FaPencilAlt: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10" /></svg>`,
    FaChild: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M12 4.5v15m7.5-7.5h-15M19.5 12a7.5 7.5 0 11-15 0 7.5 7.5 0 0115 0z" /></svg>`,
    FaUsers: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M15 19.128a9.38 9.38 0 002.625.372 9.337 9.337 0 004.121-.952 4.125 4.125 0 00-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 018.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0111.964-3.07M12 6.375a3.375 3.375 0 11-6.75 0 3.375 3.375 0 016.75 0zm8.25 2.25a2.625 2.625 0 11-5.25 0 2.625 2.625 0 015.25 0z" /></svg>`,
    FaRegCalendarCheck: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5m-9-6h.008v.008H12v-.008zM12 15h.008v.008H12V15zm0 2.25h.008v.008H12v-.008zM9.75 15h.008v.008H9.75V15zm0 2.25h.008v.008H9.75v-.008zM7.5 15h.008v.008H7.5V15zm0 2.25h.008v.008H7.5v-.008zm6.75-4.5h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V15zm0 2.25h.008v.008h-.008v-.008zm2.25-4.5h.008v.008H16.5v-.008zm0 2.25h.008v.008H16.5V15z" /></svg>`,
    FaPlaneDeparture: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6 12L3.269 3.126A59.768 59.768 0 0121.485 12 59.77 59.77 0 013.27 20.876L5.999 12zm0 0h7.5" /></svg>`,
    FaCarSide: `<svg class="h-7 w-7 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M8.25 18.75a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h6m-9 0H3.375a1.125 1.125 0 01-1.125-1.125V14.25m17.25 4.5a1.5 1.5 0 01-3 0m3 0a1.5 1.5 0 00-3 0m3 0h1.125c.621 0 1.129-.504 1.09-1.124a17.902 17.902 0 00-3.213-9.193 2.056 2.056 0 00-1.58-.86H14.25M16.5 18.75h-2.25m0-11.177v-.958c0-.568-.422-1.048-.987-1.106a48.554 48.554 0 00-10.026 0 1.106 1.106 0 00-.987 1.106v7.635m12-6.677v6.677m0 4.5v-4.5m0 0h-12" /></svg>`
};

// 2. Получение данных из PocketBase
const pb = authService.createClientFromRequest(Astro.request);

// Запрашиваем запись из коллекции 'section_how_it_works' (имя коллекции, которое вы создали)
const record = await pb.collection('how_it_works').getFirstListItem('');

// Формируем объект контента
const content = {
    mainTitle: record.title,
    subtitle: record.subtitle,
    // JSON массив шагов
    steps: record.steps || []
};
---

<section class="bg-gray-50">
    <div class="max-w-7xl mx-auto px-6 lg:px-8">
        <div class="text-center section-header">
            <h2 class="text-4xl font-bold tracking-tight text-gray-900 sm:text-5xl md:text-6xl animate-title">
                {content.mainTitle}
            </h2>
            <p class="mt-6 text-lg leading-8 text-gray-600 max-w-3xl mx-auto animate-subtitle">
                {content.subtitle}
            </p>
        </div>

        <div class="mt-24 grid grid-cols-1 gap-y-16 md:grid-cols-2 lg:grid-cols-3 md:gap-x-12 steps-container">
            {content.steps.map((step: Step, index: number) => {
                // Выбираем SVG по имени из базы. Если иконка не найдена, используем заглушку (или первую иконку).
                const iconSvg = modernIcons[step.icon as keyof typeof modernIcons] || modernIcons['FaCarSide'];

                return (
                        <div
                                class={`step-item relative p-8 bg-white rounded-2xl shadow-lg transition-all duration-300 flex flex-col items-center text-center animate-card step-${index + 1}`}
                        >
                            <div class="absolute -top-6">
                                <div
                                        class={`flex h-16 w-16 items-center justify-center rounded-full ${COLOR_CLASSES.primaryBg} shadow-md`}
                                        set:html={iconSvg}
                                >
                                </div>
                            </div>
                            <h3 class="mt-10 text-2xl font-bold leading-8 text-gray-900">
                                {step.title}
                            </h3>
                            <p class="mt-4 text-base leading-7 text-gray-600">
                                {step.description}
                            </p>
                        </div>
                );
            })}
        </div>
    </div>
</section>

<style>
    .animate-title {
        opacity: 0;
        transform: translateY(-20px);
        animation: fadeInDown 0.7s ease-out forwards;
    }

    .animate-subtitle {
        opacity: 0;
        transform: translateY(10px);
        animation: fadeInUp 0.7s ease-out 0.2s forwards;
    }

    .animate-card {
        opacity: 0;
        transform: translateY(50px);
        animation: staggerFadeIn 0.6s cubic-bezier(0.6, 0.05, 0, 0.9) forwards;
    }

    .step-item:hover {
        transform: translateY(-10px) scale(1.05);
        box-shadow: 0 25px 50px -12px rgba(251, 191, 36, 0.25);
    }

    @keyframes fadeInDown {
        to { opacity: 1; transform: translateY(0); }
    }

    @keyframes fadeInUp {
        to { opacity: 1; transform: translateY(0); }
    }

    @keyframes staggerFadeIn {
        to { opacity: 1; transform: translateY(0); }
    }

    /* Анимация задержки для карточек (до 6 элементов) */
    .step-1 { animation-delay: 0.2s; }
    .step-2 { animation-delay: 0.4s; }
    .step-3 { animation-delay: 0.6s; }
    .step-4 { animation-delay: 0.8s; }
    .step-5 { animation-delay: 1.0s; }
    .step-6 { animation-delay: 1.2s; }
</style>