---
import { casesData } from '@data/cases';
import type { Case } from '@data/cases';

interface RelatedCasesProps {
    title: string;
    subtitle: string;
    currentCaseId: number;
    limit?: number;
}

const {
    title = "Другие кейсы",
    subtitle = "Похожие дела",
    currentCaseId = 0,
    limit = 3
} = Astro.props as RelatedCasesProps;

function shuffleArray<T>(array: T[]): T[] {
    const shuffled = [...array];
    for (let i = shuffled.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
    }
    return shuffled;
}

const otherCases = casesData.filter(c => c.id !== currentCaseId);
const shuffledCases = shuffleArray(otherCases);
const relatedCases = shuffledCases.slice(0, limit);
---

<section class="related-cases">
    <div class="site-container">
        <div class="related-cases__header">
            <span class="related-cases__subtitle">{subtitle}</span>
            <h3 class="related-cases__title">{title}</h3>
        </div>
        <div class="related-cases__grid">
            {relatedCases.map((caseItem) => (
                <a href={caseItem.href} class="related-case-card">
                    <div class="related-case-card__header">
                        <span class="related-case-card__category-icon">{caseItem.categoryIcon}</span>
                        <span class="related-case-card__category">{caseItem.category}</span>
                    </div>
                    <h4 class="related-case-card__title">{caseItem.title}</h4>
                    <p class="related-case-card__desc">{caseItem.description}</p>
                    <div class="related-case-card__footer">
                        <div class="related-case-card__meta">
                            <span class="related-case-card__result">{caseItem.resultLabel}</span>
                            <span class="related-case-card__duration">{caseItem.duration}</span>
                        </div>
                        <span class="related-case-card__link">
                            Подробнее
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M5 12h14"/>
                                <path d="m12 5 7 7-7 7"/>
                            </svg>
                        </span>
                    </div>
                </a>
            ))}
        </div>
    </div>
</section>

<style>
    .related-cases {
        padding: 5rem 0;
        background: #f1f5f9;
    }

    .site-container {
        max-width: var(--site-max-width, 1400px);
        margin: 0 auto;
        padding: 0 20px;
    }

    .related-cases__header {
        text-align: center;
        margin-bottom: 3rem;
    }

    .related-cases__subtitle {
        display: inline-block;
        color: #eac26e;
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 3px;
        margin-bottom: 0.75rem;
        padding: 0.5rem 1rem;
        background: rgba(234, 194, 110, 0.1);
        border-radius: 6px;
    }

    .related-cases__title {
        color: #0a2540;
        font-size: clamp(1.5rem, 3vw, 2rem);
        font-weight: 800;
        margin: 0;
    }

    .related-cases__grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 2rem;
    }

    .related-case-card {
        background: #ffffff;
        border-radius: 16px;
        padding: 2rem;
        border: 1px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        text-decoration: none;
        transition: all 0.3s ease;
        display: flex;
        flex-direction: column;
    }

    .related-case-card:hover {
        border-color: rgba(234, 194, 110, 0.3);
        box-shadow: 0 12px 30px rgba(0, 0, 0, 0.1);
        transform: translateY(-4px);
    }

    .related-case-card__header {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    .related-case-card__category-icon {
        font-size: 1.25rem;
    }

    .related-case-card__category {
        font-size: 0.75rem;
        font-weight: 600;
        color: #ce9f40;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .related-case-card__title {
        color: #0a2540;
        font-size: 1.1rem;
        font-weight: 700;
        margin: 0 0 0.75rem 0;
        line-height: 1.4;
        transition: color 0.3s ease;
    }

    .related-case-card:hover .related-case-card__title {
        color: #eac26e;
    }

    .related-case-card__desc {
        color: #64748b;
        font-size: 0.9rem;
        line-height: 1.6;
        margin: 0 0 1.5rem 0;
        flex: 1;
    }

    .related-case-card__footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 1rem;
        border-top: 1px solid #f1f5f9;
    }

    .related-case-card__meta {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
    }

    .related-case-card__result {
        font-size: 0.85rem;
        font-weight: 700;
        color: #22c55e;
    }

    .related-case-card__duration {
        font-size: 0.8rem;
        color: #94a3b8;
    }

    .related-case-card__link {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.85rem;
        font-weight: 600;
        color: #0a2540;
        transition: color 0.3s ease;
    }

    .related-case-card__link svg {
        width: 18px;
        height: 18px;
        transition: transform 0.3s ease;
    }

    .related-case-card:hover .related-case-card__link {
        color: #eac26e;
    }

    .related-case-card:hover .related-case-card__link svg {
        transform: translateX(4px);
    }

    @media (max-width: 1024px) {
        .related-cases__grid {
            grid-template-columns: repeat(2, 1fr);
        }
    }

    @media (max-width: 768px) {
        .related-cases {
            padding: 3rem 0;
        }

        .related-cases__grid {
            grid-template-columns: 1fr;
        }

        .related-case-card__header {
            justify-content: center;
        }

        .related-case-card__title {
            text-align: center;
        }

        .related-case-card__desc {
            text-align: center;
        }

        .related-case-card__footer {
            flex-direction: column;
            align-items: center;
            text-align: center;
        }
    }
</style>