@extends('layouts.master')

@section('content')
<div>
    @include('flash::message')

    <div class="welcome-box">
        <h1>{!! $siteSettings['welcome_title'] ?? 'مرحباً بك في <span style="color: #7777CC;">كلمات</span> الأغاني!' !!}</h1>
        <p>{{ $siteSettings['welcome_subtitle'] ?? 'المكان الذي تنتهي فيه جميع عمليات البحث!' }}</p>
        <p style="color: #555; font-size: 0.9em;">{{ $siteSettings['welcome_description'] ?? 'لدينا عالم كبير ومتنامي يومياً من الكلمات حيث تتألق نجوم جميع الأنواع والأجيال.' }}</p>
    </div>

    <div style="text-align: center; margin-bottom: 20px;">
        <form method="get" action="/search" style="display: inline-flex; gap: 5px; justify-content: center; width: 100%; max-width: 500px; margin: 0 auto;">
            <input type="search" placeholder="ادخل اسم الفنان أو عنوان الأغنية" name="q" required style="flex: 1; min-width: 0; padding: 6px 12px; border: 1px inset #EEE; background: #fff; color: #000;">
            <button type="submit" style="background: #7777CC; color: #fff; border: 1px outset #9999CC; padding: 6px 16px; font-weight: 700; cursor: pointer; white-space: nowrap;">بحث</button>
        </form>
    </div>

    <hr>

    {{-- Most Visited Songs Section --}}
    @php
        $mostVisitedSongs = \App\Song::with('artists')->orderBy('views', 'desc')->take(60)->get();
    @endphp
    @if($mostVisitedSongs->count())
    <div style="margin: 20px 0;">
        <h2 style="text-align: center; color: #7777CC; margin-bottom: 20px; font-size: 1.6em;">كلمات الأغاني الأكثر زيارة</h2>
        <div class="most-visited-grid" style="display: flex; flex-wrap: wrap; gap: 12px; justify-content: center;">
            @foreach($mostVisitedSongs as $index => $song)
                <div class="most-visited-item" style="display: flex; justify-content: space-between; align-items: center; background: #e8e8f0; border: 1px solid #c8c8dd; padding: 8px 10px; border-radius: 4px; width: calc(33.333% - 8px); min-width: 200px; box-sizing: border-box; font-size: 0.9em;">
                    <div style="display: flex; align-items: center; gap: 8px; overflow: hidden; flex: 1;">
                        <span style="color: #666; font-weight: bold; min-width: 30px;">{{ $index + 1 }}.</span>
                        <a href="/lyrics/{{ $song->slug }}" style="color: #a80000; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
                            {{ $song->title }}
                        </a>
                        <span style="color: #888; white-space: nowrap;">- {{ $song->artists->first()->name ?? 'فنان' }}</span>
                    </div>
                    <div style="display: flex; align-items: center; gap: 4px; color: #666; white-space: nowrap;">
                        <span>{{ number_format($song->views) }}</span>
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="vertical-align: middle;">
                            <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                            <circle cx="12" cy="12" r="3"></circle>
                        </svg>
                    </div>
                </div>
            @endforeach
        </div>
    </div>
    @endif

    <hr>

    <div style="text-align: center; margin: 10px 0;">
        <a href="/songs/create" class="btn btn-primary">إرسال كلمات أغنية</a>
    </div>

    <hr>

    @if(($siteSettings['stats_enabled'] ?? '1') == '1')
    @php
        $statsSongs = is_numeric($siteSettings['stats_songs'] ?? null) ? (int) $siteSettings['stats_songs'] : DB::table('songs')->count();
        $statsArtists = is_numeric($siteSettings['stats_artists'] ?? null) ? (int) $siteSettings['stats_artists'] : DB::table('artists')->count();
        $statsUsers = is_numeric($siteSettings['stats_users'] ?? null) ? (int) $siteSettings['stats_users'] : DB::table('users')->count();
        $statsViews = is_numeric($siteSettings['stats_views'] ?? null) ? (int) $siteSettings['stats_views'] : DB::table('songs')->sum('views');
    @endphp

    <div style="margin: 25px 0;">
        <div style="background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); color: #fff; padding: 12px; text-align: center; font-weight: 700; font-size: 1.25em; border-radius: 10px 10px 0 0; box-shadow: 0 4px 15px rgba(119,119,204,0.3);">
            <i class="fa fa-chart-bar" style="margin-left: 8px;"></i> احصائيات الموقع
        </div>
        <div style="background: #fff; border: 2px solid #9999CC; border-top: none; border-radius: 0 0 10px 10px; padding: 20px; box-shadow: 0 4px 15px rgba(0,0,0,0.08);">
            <div style="display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px;">
                
                <div style="background: linear-gradient(145deg, #f8f8ff 0%, #f0f0fa 100%); border-radius: 12px; padding: 16px 6px; text-align: center; border: 1px solid #e0e0f0; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-5px)';this.style.boxShadow='0 8px 20px rgba(119,119,204,0.25)';" onmouseout="this.style.transform='translateY(0)';this.style.boxShadow='none';">
                    <div style="width: 55px; height: 55px; background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 12px; box-shadow: 0 4px 10px rgba(119,119,204,0.3);">
                        <i class="fa fa-music" style="font-size: 1.6em; color: #fff;"></i>
                    </div>
                    <div style="color: #555; font-weight: 600; font-size: 0.95em; margin-bottom: 6px;">الأغاني</div>
                    <div style="color: #7777CC; font-weight: 800; font-size: 1.5em;">{{ number_format($statsSongs) }}</div>
                </div>

                <div style="background: linear-gradient(145deg, #f8f8ff 0%, #f0f0fa 100%); border-radius: 12px; padding: 16px 6px; text-align: center; border: 1px solid #e0e0f0; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-5px)';this.style.boxShadow='0 8px 20px rgba(119,119,204,0.25)';" onmouseout="this.style.transform='translateY(0)';this.style.boxShadow='none';">
                    <div style="width: 50px; height: 50px; background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 10px; box-shadow: 0 4px 10px rgba(119,119,204,0.3);">
                        <i class="fa fa-user" style="font-size: 1.5em; color: #fff;"></i>
                    </div>
                    <div style="color: #555; font-weight: 600; font-size: 0.9em; margin-bottom: 5px;">المطربين</div>
                    <div style="color: #7777CC; font-weight: 800; font-size: 1.4em;">{{ number_format($statsArtists) }}</div>
                </div>

                <div style="background: linear-gradient(145deg, #f8f8ff 0%, #f0f0fa 100%); border-radius: 12px; padding: 16px 6px; text-align: center; border: 1px solid #e0e0f0; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-5px)';this.style.boxShadow='0 8px 20px rgba(119,119,204,0.25)';" onmouseout="this.style.transform='translateY(0)';this.style.boxShadow='none';">
                    <div style="width: 50px; height: 50px; background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 10px; box-shadow: 0 4px 10px rgba(119,119,204,0.3);">
                        <i class="fa fa-users" style="font-size: 1.3em; color: #fff;"></i>
                    </div>
                    <div style="color: #555; font-weight: 600; font-size: 0.9em; margin-bottom: 5px;">حسابات الفنانين</div>
                    <div style="color: #7777CC; font-weight: 800; font-size: 1.4em;">{{ number_format($statsUsers) }}</div>
                </div>

                <div style="background: linear-gradient(145deg, #f8f8ff 0%, #f0f0fa 100%); border-radius: 12px; padding: 16px 6px; text-align: center; border: 1px solid #e0e0f0; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-5px)';this.style.boxShadow='0 8px 20px rgba(119,119,204,0.25)';" onmouseout="this.style.transform='translateY(0)';this.style.boxShadow='none';">
                    <div style="width: 50px; height: 50px; background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 10px; box-shadow: 0 4px 10px rgba(119,119,204,0.3);">
                        <i class="fa fa-eye" style="font-size: 1.5em; color: #fff;"></i>
                    </div>
                    <div style="color: #555; font-weight: 600; font-size: 0.9em; margin-bottom: 5px;">مشاهدات</div>
                    <div style="color: #7777CC; font-weight: 800; font-size: 1.4em;">{{ number_format($statsViews) }}</div>
                </div>

                <div style="background: linear-gradient(145deg, #f8f8ff 0%, #f0f0fa 100%); border-radius: 12px; padding: 16px 6px; text-align: center; border: 1px solid #e0e0f0; transition: transform 0.2s, box-shadow 0.2s;" onmouseover="this.style.transform='translateY(-5px)';this.style.boxShadow='0 8px 20px rgba(119,119,204,0.25)';" onmouseout="this.style.transform='translateY(0)';this.style.boxShadow='none';">
                    <div style="width: 50px; height: 50px; background: linear-gradient(135deg, #7777CC 0%, #9999CC 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 10px; box-shadow: 0 4px 10px rgba(119,119,204,0.3);">
                        <i class="fa fa-pencil" style="font-size: 1.4em; color: #fff;"></i>
                    </div>
                    <div style="color: #555; font-weight: 600; font-size: 0.9em; margin-bottom: 5px;">المساهمين</div>
                    <div style="color: #7777CC; font-weight: 800; font-size: 1.4em;">{{ number_format($statsUsers) }}</div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <hr>

</div>
@endsection
