@extends('theme-default::layouts.base')
@section('title', AleshaSetting::get('site_name', 'Home'))

@php
    $siteName     = AleshaSetting::get('site_name', 'AleshaCMS');
    $tagline      = AleshaSetting::get('site_tagline', 'Powered by AleshaCMS');
    $posts        = AleshaPost::ofType('post')->published()->with('taxonomies', 'author')->latest('published_at')->take(6)->get();
    $featured     = $posts->first();
    $rest         = $posts->skip(1);
    $slidersActive = AleshaPlugin::where('slug', 'Sliders')->where('is_active', true)->exists();
    $heroSlider    = $slidersActive
        ? AleshaSlider::with('activeSlides')->get()->first(fn($s) => $s->activeSlides->isNotEmpty())
        : null;
@endphp

@section('content')

{{-- Hero: slider if available, else gradient fallback --}}
@if($heroSlider)
    @php $slider_id = $heroSlider->id @endphp
    @include('theme-default::components.builder.slider')
@else
<section class="relative overflow-hidden bg-gradient-to-br from-indigo-950 via-indigo-900 to-violet-900 text-white">
    <div class="absolute inset-0 opacity-20"
         style="background-image: radial-gradient(circle at 20% 50%, #6366f1 0%, transparent 50%), radial-gradient(circle at 80% 20%, #8b5cf6 0%, transparent 50%)">
    </div>
    <div class="relative max-w-6xl mx-auto px-4 sm:px-6 py-24 sm:py-32">
        <div class="max-w-2xl">
            <span class="tag bg-white/10 text-indigo-200 mb-6 inline-flex">Welcome</span>
            <h1 class="text-4xl sm:text-5xl lg:text-6xl font-black leading-tight mb-6">
                {{ $siteName }}
            </h1>
            <p class="text-lg sm:text-xl text-indigo-200 leading-relaxed mb-8">
                {{ $tagline }}
            </p>
            @if($featured)
            <a href="/{{ $featured->content_type }}/{{ $featured->slug }}" class="btn-primary bg-white text-indigo-700 hover:bg-indigo-50">
                Read latest post
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/>
                </svg>
            </a>
            @endif
        </div>
    </div>
</section>
@endif

@if($posts->count())
<section class="max-w-6xl mx-auto px-4 sm:px-6 py-20">

    {{-- Featured Post --}}
    @if($featured)
    <div class="mb-16">
        <div class="flex items-center gap-3 mb-8">
            <h2 class="text-2xl font-bold text-gray-900">Featured</h2>
            <div class="flex-1 h-px bg-gray-100"></div>
        </div>
        <article class="card md:flex group">
            @if($featured->getFirstMedia('featured'))
            <div class="md:w-2/5 flex-shrink-0">
                <img src="{{ $featured->getFirstMediaUrl('featured') }}" alt="{{ $featured->title }}"
                     class="w-full h-56 md:h-full object-cover">
            </div>
            @endif
            <div class="p-8 flex flex-col justify-center">
                <div class="flex flex-wrap gap-2 mb-4">
                    <span class="tag">{{ ucfirst($featured->content_type) }}</span>
                    @foreach($featured->taxonomies->where('type', 'category') as $cat)
                    <a href="/category/{{ $cat->slug }}" class="tag bg-indigo-50 text-indigo-600 hover:bg-indigo-100 transition-colors">{{ $cat->name }}</a>
                    @endforeach
                </div>
                <h3 class="text-2xl font-bold text-gray-900 mb-3 group-hover:text-indigo-600 transition-colors leading-snug">
                    <a href="/{{ $featured->content_type }}/{{ $featured->slug }}">{{ $featured->title }}</a>
                </h3>
                @if($featured->excerpt)
                <p class="text-gray-500 leading-relaxed mb-6">{{ $featured->excerpt }}</p>
                @endif
                <div class="flex items-center justify-between">
                    <div class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-700 font-semibold text-sm">
                            {{ substr($featured->author->name ?? 'A', 0, 1) }}
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-700">{{ $featured->author->name ?? 'Admin' }}</p>
                            <p class="text-xs text-gray-400">{{ $featured->published_at?->format('M d, Y') }}</p>
                        </div>
                    </div>
                    <a href="/{{ $featured->content_type }}/{{ $featured->slug }}" class="text-sm font-semibold text-indigo-600 hover:text-indigo-700 flex items-center gap-1">
                        Read more
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/>
                        </svg>
                    </a>
                </div>
            </div>
        </article>
    </div>
    @endif

    {{-- Post Grid --}}
    @if($rest->count())
    <div>
        <div class="flex items-center gap-3 mb-8">
            <h2 class="text-2xl font-bold text-gray-900">Latest Posts</h2>
            <div class="flex-1 h-px bg-gray-100"></div>
            <a href="/post" class="text-sm text-indigo-600 hover:text-indigo-700 font-medium">View all →</a>
        </div>
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
            @foreach($rest as $post)
            <article class="card group flex flex-col">
                @if($post->getFirstMedia('featured'))
                <div class="overflow-hidden">
                    <img src="{{ $post->getFirstMediaUrl('featured') }}" alt="{{ $post->title }}"
                         class="w-full h-48 object-cover group-hover:scale-105 transition-transform duration-300">
                </div>
                @else
                <div class="h-2 bg-gradient-to-r from-indigo-500 to-violet-500"></div>
                @endif
                <div class="p-6 flex flex-col flex-1">
                    <div class="flex flex-wrap items-center gap-2 mb-3">
                        @php $cat = $post->taxonomies->where('type', 'category')->first() @endphp
                        @if($cat)
                        <a href="/category/{{ $cat->slug }}" class="tag bg-indigo-50 text-indigo-600 hover:bg-indigo-100 transition-colors">{{ $cat->name }}</a>
                        @else
                        <span class="tag">{{ ucfirst($post->content_type) }}</span>
                        @endif
                        <span class="text-xs text-gray-400">{{ $post->published_at?->format('M d') }}</span>
                    </div>
                    <h3 class="font-bold text-gray-900 mb-2 leading-snug group-hover:text-indigo-600 transition-colors flex-1">
                        <a href="/{{ $post->content_type }}/{{ $post->slug }}">{{ $post->title }}</a>
                    </h3>
                    @if($post->excerpt)
                    <p class="text-sm text-gray-500 line-clamp-2 leading-relaxed mb-4">{{ $post->excerpt }}</p>
                    @endif
                    <div class="flex items-center gap-2 pt-4 border-t border-gray-50 mt-auto">
                        <div class="w-6 h-6 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-700 font-semibold text-xs">
                            {{ substr($post->author->name ?? 'A', 0, 1) }}
                        </div>
                        <span class="text-xs text-gray-400">{{ $post->author->name ?? 'Admin' }}</span>
                    </div>
                </div>
            </article>
            @endforeach
        </div>
    </div>
    @endif

</section>
@else
<section class="max-w-2xl mx-auto px-4 py-24 text-center">
    <div class="w-16 h-16 bg-indigo-50 rounded-2xl flex items-center justify-center mx-auto mb-6">
        <svg class="w-8 h-8 text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
        </svg>
    </div>
    <h2 class="text-xl font-semibold text-gray-900 mb-2">No posts yet</h2>
    <p class="text-gray-500">Check back soon for new content.</p>
</section>
@endif

@endsection
