---
import Button from '@components/base/Button.astro';
import { Icon } from 'astro-icon/components';

interface PageHeroProps {
    badgeText: string;
    titleWhite: string;
    titleGold: string;
    description: string;
    btnText?: string;
    btnHref?: string;
    bgImage?: string;
    minHeight?: string;
    layout?: 'default' | 'with-image';
    sideImage?: string;
    sideImageAlt?: string;
    experienceBadge?: {
        number: string;
        text: string;
    };
    icon?: string; // Название иконки из /src/icons/
}

const {
    badgeText,
    titleWhite,
    titleGold,
    description,
    btnText,
    btnHref = "#contact",
    bgImage = "",
    minHeight = "80vh",
    layout = "default",
    sideImage = "",
    sideImageAlt = "",
    experienceBadge,
    icon
} = Astro.props as PageHeroProps;

const showImage = layout === 'with-image' && sideImage;
---

<section class="hero-section" style:min-height={minHeight}>
    <div class="hero-overlay"></div>

    <div class="site-container hero-grid">
        <div class="hero-content">
            <div class="badge animate-load" data-delay="100">
                {icon ? (
                    <span class="badge-icon">
                        <Icon name={icon} />
                    </span>
                ) : (
                    <span class="status-dot"></span>
                )}
                {badgeText}
            </div>

            <h1 class="hero-title">
                <span class="text-white animate-load" data-delay="300">{titleWhite}</span>
                <br />
                <span class="text-gold animate-load" data-delay="500">{titleGold}</span>
            </h1>

            <p class="hero-description animate-load" data-delay="700">{description}</p>

            {btnText && (
                <div class="hero-actions animate-load" data-delay="900">
                    <Button variant="gold" size="lg" href={btnHref}>
                        {btnText}
                    </Button>
                </div>
            )}
        </div>

        {showImage && (
            <div class="hero-image-wrapper animate-load" data-delay="400">
                <div class="image-composition">
                    <img src={sideImage} alt={sideImageAlt || "Юрист"} class="main-image" width="380" height="500" />
                    {experienceBadge && (
                        <div class="experience-badge animate-load" data-delay="1100">
                            <span class="exp-number">{experienceBadge.number}</span>
                            <span class="exp-text">{experienceBadge.text}</span>
                        </div>
                    )}
                </div>
            </div>
        )}
    </div>
</section>

<script>
    const initAnimations = () => {
        const animatedElements = document.querySelectorAll('.animate-load');

        animatedElements.forEach((el) => {
            const delay = parseInt((el as HTMLElement).dataset.delay || '0');

            setTimeout(() => {
                el.classList.add('is-visible');
            }, delay);
        });
    };

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initAnimations);
    } else {
        initAnimations();
    }
</script>

<style define:vars={{ bgImage: bgImage ? `url("${bgImage}")` : "none" }}>
    .hero-section {
        position: relative;
        width: 100%;
        min-height: 80vh;
        display: flex;
        align-items: center;
        background-image: var(--bgImage);
        background-size: cover;
        background-position: center;
        background-repeat: no-repeat;
        margin: 0 !important;
        padding-top: 80px;
        box-sizing: border-box;
        overflow: hidden;
    }

    .hero-overlay {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, #0a2540 0%, rgba(10, 37, 64, 0.9) 50%, rgba(10, 37, 64, 0.7) 100%);
        z-index: 1;
    }

    .site-container {
        position: relative;
        z-index: 2;
        max-width: 1200px;
        margin: 0 auto;
        padding: 0 20px;
        width: 100%;
    }

    /* --- GRID LAYOUT --- */
    .hero-grid {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 3rem;
        width: 100%;
        padding-bottom: 4rem;
    }

    .hero-content {
        flex: 1.2;
        max-width: 700px;
    }

    /* --- АНИМАЦИИ ПРИ ЗАГРУЗКЕ --- */
    .animate-load {
        opacity: 0;
        transform: translateY(30px);
        transition: opacity 0.8s cubic-bezier(0.4, 0, 0.2, 1),
                    transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
        will-change: opacity, transform;
    }

    .animate-load.is-visible {
        opacity: 1;
        transform: translateY(0);
    }

    /* Специальная анимация для изображения */
    .hero-image-wrapper.animate-load {
        opacity: 0;
        transform: translateX(50px) scale(0.95);
        transition: opacity 1s cubic-bezier(0.4, 0, 0.2, 1),
                    transform 1s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .hero-image-wrapper.animate-load.is-visible {
        opacity: 1;
        transform: translateX(0) scale(1);
    }

    /* Анимация для бейджа опыта */
    .experience-badge.animate-load {
        opacity: 0;
        transform: translateY(20px) scale(0.9);
        transition: opacity 0.6s cubic-bezier(0.4, 0, 0.2, 1),
                    transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .experience-badge.animate-load.is-visible {
        opacity: 1;
        transform: translateY(0) scale(1);
    }

    /* --- BADGE --- */
    .badge {
        display: inline-flex;
        align-items: center;
        gap: 0.6rem;
        background-color: rgba(234, 194, 110, 0.15);
        border: 1px solid rgba(234, 194, 110, 0.3);
        color: #eac26e;
        padding: 0.5rem 1rem;
        border-radius: 6px;
        font-size: 0.8rem;
        font-weight: 700;
        letter-spacing: 1.5px;
        margin-bottom: 2.5rem;
    }

    .badge svg {
        color: #eac26e;
        flex-shrink: 0;
    }

    /* Мерцающая точка (только на главной) */
    .status-dot {
        width: 10px;
        height: 10px;
        background: #22c55e;
        border-radius: 50%;
        animation: pulse 2s ease-in-out infinite;
        box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7);
        flex-shrink: 0;
    }

    .badge-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    .badge-icon svg {
        width: 20px !important;
        height: 20px !important;
        stroke-width: 2 !important;
    }

    @keyframes pulse {
        0% {
            transform: scale(0.95);
            box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7);
        }
        70% {
            transform: scale(1);
            box-shadow: 0 0 0 6px rgba(34, 197, 94, 0);
        }
        100% {
            transform: scale(0.95);
            box-shadow: 0 0 0 0 rgba(34, 197, 94, 0);
        }
    }

    /* --- TITLE --- */
    .hero-title {
        font-size: clamp(2rem, 5vw, 3.8rem);
        font-weight: 800;
        line-height: 1.15;
        margin: 0 0 2rem 0;
    }

    .text-white {
        color: #ffffff;
    }

    .text-gold {
        color: #eac26e;
    }

    /* --- DESCRIPTION --- */
    .hero-description {
        color: rgba(255, 255, 255, 0.85);
        font-size: 1.15rem;
        line-height: 1.6;
        margin-bottom: 3.5rem;
        max-width: 580px;
    }

    /* --- ACTIONS --- */
    .hero-actions {
        display: flex;
        gap: 1.5rem;
        align-items: stretch;
    }

    .hero-actions > * {
        display: flex;
        align-items: center;
    }

    /* --- IMAGE BLOCK --- */
    .hero-image-wrapper {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        position: relative;
    }

    .image-composition {
        position: relative;
        width: 100%;
        max-width: 480px;
    }

    .image-composition::before {
        content: '';
        position: absolute;
        top: -10px;
        left: 20px;
        right: -20px;
        bottom: 10px;
        background-color: #ffffff;
        border-radius: 12px;
        transform: rotate(3deg);
        z-index: -1;
        opacity: 0;
        animation: fadeInRotate 0.8s ease 0.6s forwards;
    }

    @keyframes fadeInRotate {
        from {
            opacity: 0;
            transform: rotate(0deg) translateY(20px);
        }
        to {
            opacity: 1;
            transform: rotate(3deg) translateY(0);
        }
    }

    .main-image {
        width: 100%;
        border-radius: 12px;
        border: 8px solid #ffffff;
        filter: grayscale(100%);
        box-shadow: 0 30px 60px rgba(0,0,0,0.4);
    }

    .experience-badge {
        position: absolute;
        bottom: 30px;
        left: -30px;
        background: #ffffff;
        padding: 1.5rem;
        border-radius: 8px;
        box-shadow: 0 15px 35px rgba(0,0,0,0.2);
        min-width: 180px;
    }

    .exp-number {
        font-size: 2.2rem;
        font-weight: 900;
        color: #1e3050;
        line-height: 1;
        display: block;
    }

    .exp-text {
        font-size: 0.7rem;
        font-weight: 800;
        color: #535e6c;
        text-transform: uppercase;
    }

    /* --- МОБИЛЬНАЯ АДАПТАЦИЯ --- */
    @media (max-width: 992px) {
        .hero-section {
            padding-top: 70px;
            min-height: 60vh;
        }

        .hero-grid {
            flex-direction: column;
            text-align: center;
            padding-top: 0;
        }

        .hero-content {
            align-items: center;
        }

        .hero-actions {
            justify-content: center;
        }

        .hero-image-wrapper {
            margin-top: 3rem;
            justify-content: center;
        }

        .experience-badge {
            left: 0;
            bottom: -20px;
        }

        .hero-image-wrapper.animate-load {
            transform: translateY(40px) scale(0.95);
        }

        .hero-image-wrapper.animate-load.is-visible {
            transform: translateY(0) scale(1);
        }
    }

    @media (max-width: 767px) {
        .hero-section {
            padding-top: 64px;
        }

        .hero-title {
            font-size: 1.8rem;
        }

        .hero-actions {
            flex-direction: column;
            width: 100%;
        }

        .hero-actions > * {
            width: 100%;
        }

        .image-composition::before {
            display: none;
        }
    }

    @media (max-width: 480px) {
        .hero-section {
            padding-top: 32px;
        }

        .hero-title {
            font-size: 1.8rem;
        }
    }

    /* Уважаем prefers-reduced-motion */
    @media (prefers-reduced-motion: reduce) {
        .animate-load,
        .hero-image-wrapper.animate-load,
        .experience-badge.animate-load {
            opacity: 1;
            transform: none;
            transition: none;
        }

        .image-composition::before {
            animation: none;
            opacity: 1;
        }
    }
</style>
