{{-- resources/views/siswa/recommendations.blade.php --}}
@extends('layouts.app')

@section('title', 'Rekomendasi Belajar - LearnMood')

@section('content')
<div class="space-y-6 pb-16 md:pb-0">
    
    <!-- Header -->
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h2 class="text-xl md:text-2xl font-bold text-gray-800">Rekomendasi Belajar</h2>
            <p class="text-sm md:text-base text-gray-500 mt-1">Personalisasi berdasarkan aktivitas dan mood harianmu</p>
        </div>
        
        <!-- Info Mode -->
        @if(isset($todayRecommendation))
            <div class="bg-blue-50 border border-blue-200 rounded-lg px-3 md:px-4 py-2">
                <span class="text-xs md:text-sm text-blue-700">
                    @if($todayRecommendation->based_on == 'daily_model')
                        🤖 Model Harian
                    @elseif($todayRecommendation->based_on == 'student_pattern')
                        📊 Pola Siswa
                    @else
                        📝 Aturan Manual
                    @endif
                </span>
            </div>
        @endif
    </div>

    <!-- Today's Recommendation Card -->
    @if($todayRecommendation)
        <div class="bg-white rounded-2xl shadow-lg border border-gray-100 overflow-hidden">
            <!-- Header Card -->
            <div class="bg-gradient-to-r 
                @if($todayRecommendation->category == 'Ringan') from-yellow-500 to-yellow-600
                @elseif($todayRecommendation->category == 'Sedang') from-green-500 to-green-600
                @else from-blue-500 to-blue-600 @endif
                px-4 md:px-6 py-4">
                <div class="flex justify-between items-center">
                    <div>
                        <span class="text-white/80 text-xs md:text-sm">Rekomendasi Hari Ini</span>
                        <h3 class="text-xl md:text-2xl font-bold text-white mt-1">
                            {{-- ✅ DIUBAH --}}
                            {{ $todayRecommendation->display_category }}
                        </h3>
                    </div>
                    <div class="bg-white/20 p-2 md:p-3 rounded-xl">
                        <svg class="w-6 h-6 md:w-8 md:h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                        </svg>
                    </div>
                </div>
            </div>

            <!-- Content -->
            <div class="p-4 md:p-6">
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-4 md:gap-6">
                    <!-- Left Column -->
                    <div class="space-y-4">
                        <!-- Durasi -->
                        <div class="flex items-center justify-between p-3 md:p-4 bg-gray-50 rounded-xl">
                            <div class="flex items-center space-x-2 md:space-x-3">
                                <div class="w-8 h-8 md:w-10 md:h-10 bg-blue-100 rounded-lg flex items-center justify-center shrink-0">
                                    <svg class="w-4 h-4 md:w-5 md:h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                    </svg>
                                </div>
                                <div>
                                    <p class="text-xs md:text-sm text-gray-500">Durasi yang Disarankan</p>
                                    <p class="text-lg md:text-xl font-bold text-gray-800">{{ $todayRecommendation->recommended_minutes }} Menit</p>
                                </div>
                            </div>
                            <span class="text-xs md:text-sm text-gray-400">/hari</span>
                        </div>

                        <!-- Aktivitas Hari Ini -->
                        @if($todayRecommendation->activityLog)
                        <div class="p-3 md:p-4 bg-gray-50 rounded-xl">
                            <p class="text-xs md:text-sm text-gray-500 mb-2">Berdasarkan Aktivitas Hari Ini:</p>
                            <div class="space-y-1.5 md:space-y-2 text-xs md:text-sm">
                                <div class="flex justify-between items-center">
                                    <span class="text-gray-600">Mood:</span>
                                    <span class="font-medium px-2 py-0.5 rounded-full text-xs 
                                        @if($todayRecommendation->activityLog->mood == 'Bagus') bg-green-100 text-green-700
                                        @elseif($todayRecommendation->activityLog->mood == 'Lumayan') bg-blue-100 text-blue-700
                                        @elseif($todayRecommendation->activityLog->mood == 'Biasa Saja') bg-gray-100 text-gray-700
                                        @elseif($todayRecommendation->activityLog->mood == 'Cukup Jenuh') bg-yellow-100 text-yellow-700
                                        @else bg-red-100 text-red-700 @endif">
                                        {{ $todayRecommendation->activityLog->mood }}
                                    </span>
                                </div>
                                <div class="flex justify-between">
                                    <span class="text-gray-600">Durasi Belajar:</span>
                                    <span class="font-medium">{{ $todayRecommendation->activityLog->duration_minutes }} menit</span>
                                </div>
                                <div class="flex justify-between">
                                    <span class="text-gray-600">Jam Tidur:</span>
                                    <span class="font-medium">{{ $todayRecommendation->activityLog->sleep_hours }} jam</span>
                                </div>
                            </div>
                        </div>
                        @endif
                    </div>

                    <!-- Right Column -->
                    <div class="space-y-4">
                        <!-- Notes -->
                        <div class="p-3 md:p-4 bg-gradient-to-br from-purple-50 to-blue-50 rounded-xl">
                            <div class="flex items-start space-x-2 md:space-x-3">
                                <div class="text-xl md:text-2xl shrink-0">💡</div>
                                <div>
                                    <p class="font-medium text-gray-800 mb-1 text-sm md:text-base">Catatan untukmu:</p>
                                    <p class="text-gray-600 text-xs md:text-sm">{{ $todayRecommendation->notes }}</p>
                                </div>
                            </div>
                        </div>

                        <!-- Pattern Analysis -->
                        @if($todayRecommendation->based_on == 'student_pattern' && isset($todayRecommendation->model_input['pattern_analysis']))
                            @php $pattern = $todayRecommendation->model_input['pattern_analysis']; @endphp
                            <div class="p-3 md:p-4 bg-gray-50 rounded-xl">
                                <p class="text-xs md:text-sm font-medium text-gray-700 mb-2 md:mb-3">📊 Analisis Pola 7 Hari:</p>
                                <div class="grid grid-cols-2 gap-2 text-xs md:text-sm">
                                    <div class="bg-white p-2 rounded-lg text-center">
                                        <p class="text-[10px] md:text-xs text-gray-500">Rata-rata Durasi</p>
                                        <p class="font-bold text-gray-800">{{ $pattern['avg_duration'] ?? '-' }} mnt</p>
                                    </div>
                                    <div class="bg-white p-2 rounded-lg text-center">
                                        <p class="text-[10px] md:text-xs text-gray-500">Konsistensi</p>
                                        <p class="font-bold text-gray-800">{{ $pattern['consistency'] ?? '-' }}</p>
                                    </div>
                                    <div class="bg-white p-2 rounded-lg text-center">
                                        <p class="text-[10px] md:text-xs text-gray-500">Mood Terbanyak</p>
                                        <p class="font-bold text-gray-800">{{ $pattern['most_frequent_mood'] ?? '-' }}</p>
                                    </div>
                                    <div class="bg-white p-2 rounded-lg text-center">
                                        <p class="text-[10px] md:text-xs text-gray-500">Trend</p>
                                        <p class="font-bold text-gray-800">{{ $pattern['trend'] ?? '-' }}</p>
                                    </div>
                                </div>
                            </div>
                        @endif

                        <!-- Confidence -->
                        @if($todayRecommendation->confidence)
                        <div class="p-3 md:p-4 bg-gray-50 rounded-xl">
                            <p class="text-xs md:text-sm text-gray-600 mb-2">Tingkat Keyakinan Model:</p>
                            <div class="w-full bg-gray-200 rounded-full h-2 md:h-2.5">
                                <div class="bg-blue-600 h-2 md:h-2.5 rounded-full" style="width: {{ $todayRecommendation->confidence * 100 }}%"></div>
                            </div>
                            <p class="text-right text-[10px] md:text-xs text-gray-500 mt-1">{{ round($todayRecommendation->confidence * 100) }}%</p>
                        </div>
                        @endif
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="flex flex-col sm:flex-row gap-3 mt-4 md:mt-6 pt-4 border-t">
                    <a href="{{ route('siswa.input.edit-today') }}" class="sm:flex-1 bg-blue-600 text-white text-center px-4 py-2 md:py-3 rounded-lg hover:bg-blue-700 transition-colors text-sm md:text-base">
                        Edit Aktivitas Hari Ini
                    </a>
                    <a href="{{ route('siswa.visualization') }}" class="sm:flex-1 px-4 py-2 md:py-3 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition-colors text-sm md:text-base text-center">
                        Lihat Visualisasi
                    </a>
                </div>
            </div>
        </div>
    @else
        <!-- No Recommendation Today -->
        <div class="bg-white rounded-2xl shadow-lg border border-gray-100 p-6 md:p-8 text-center">
            <div class="w-16 h-16 md:w-20 md:h-20 bg-yellow-100 rounded-full flex items-center justify-center mx-auto mb-4">
                <svg class="w-8 h-8 md:w-10 md:h-10 text-yellow-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
                </svg>
            </div>
            <h3 class="text-lg md:text-xl font-semibold text-gray-800 mb-2">Belum Ada Rekomendasi Hari Ini</h3>
            <p class="text-sm md:text-base text-gray-500 mb-6">Input aktivitas belajarmu dulu untuk mendapatkan rekomendasi personal!</p>
            <a href="{{ route('siswa.input') }}" class="inline-flex items-center px-4 md:px-6 py-2 md:py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors text-sm md:text-base">
                <svg class="w-4 h-4 md:w-5 md:h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                </svg>
                Input Sekarang
            </a>
        </div>
    @endif

    <!-- Stats Cards -->
    @if(isset($stats))
    <div class="grid grid-cols-1 sm:grid-cols-3 gap-3 md:gap-4">
        <div class="bg-white rounded-xl p-3 md:p-4 shadow-sm border border-gray-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-xs md:text-sm text-gray-500">Ringan</p>
                    <p class="text-xl md:text-2xl font-bold text-yellow-600">{{ $stats['ringan'] }}</p>
                </div>
                <div class="w-8 h-8 md:w-10 md:h-10 bg-yellow-100 rounded-lg flex items-center justify-center">
                    <span class="text-yellow-600 font-bold text-sm md:text-base">R</span>
                </div>
            </div>
            <p class="text-[10px] md:text-xs text-gray-400 mt-2">dalam 7 hari terakhir</p>
        </div>
        
        <div class="bg-white rounded-xl p-3 md:p-4 shadow-sm border border-gray-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-xs md:text-sm text-gray-500">Sedang</p>
                    <p class="text-xl md:text-2xl font-bold text-green-600">{{ $stats['sedang'] }}</p>
                </div>
                <div class="w-8 h-8 md:w-10 md:h-10 bg-green-100 rounded-lg flex items-center justify-center">
                    <span class="text-green-600 font-bold text-sm md:text-base">S</span>
                </div>
            </div>
            <p class="text-[10px] md:text-xs text-gray-400 mt-2">dalam 7 hari terakhir</p>
        </div>
        
        <div class="bg-white rounded-xl p-3 md:p-4 shadow-sm border border-gray-100">
            <div class="flex items-center justify-between">
                <div>
                    <p class="text-xs md:text-sm text-gray-500">Intensif</p>
                    <p class="text-xl md:text-2xl font-bold text-blue-600">{{ $stats['intensif'] }}</p>
                </div>
                <div class="w-8 h-8 md:w-10 md:h-10 bg-blue-100 rounded-lg flex items-center justify-center">
                    <span class="text-blue-600 font-bold text-sm md:text-base">I</span>
                </div>
            </div>
            <p class="text-[10px] md:text-xs text-gray-400 mt-2">dalam 7 hari terakhir</p>
        </div>
    </div>
    @endif

    <!-- Riwayat Rekomendasi -->
    @if(isset($weekRecommendations) && $weekRecommendations->count() > 0)
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-4 md:px-6 py-3 md:py-4 border-b border-gray-100">
            <h3 class="text-base md:text-lg font-semibold text-gray-800">Riwayat Rekomendasi 7 Hari Terakhir</h3>
        </div>
        <div class="divide-y divide-gray-100">
            @foreach($weekRecommendations as $rec)
            <div class="p-3 md:p-4 hover:bg-gray-50 transition-colors">
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
                    <div class="flex items-center space-x-2 md:space-x-4">
                        <!-- Icon berdasarkan kategori -->
                        <div class="w-8 h-8 md:w-10 md:h-10 rounded-lg flex items-center justify-center shrink-0
                            @if($rec->category == 'Ringan') bg-yellow-100
                            @elseif($rec->category == 'Sedang') bg-green-100
                            @else bg-blue-100 @endif">
                            @if($rec->category == 'Ringan')
                                <span class="text-yellow-600 font-bold text-sm md:text-base">😌</span>
                            @elseif($rec->category == 'Sedang')
                                <span class="text-green-600 font-bold text-sm md:text-base">😊</span>
                            @else
                                <span class="text-blue-600 font-bold text-sm md:text-base">🚀</span>
                            @endif
                        </div>
                        
                        <div>
                            <div class="flex items-center flex-wrap gap-2">
                                {{-- ✅ DIUBAH --}}
                                <span class="font-medium text-gray-800 text-sm md:text-base">{{ $rec->display_category }}</span>
                                <span class="text-[10px] md:text-xs px-2 py-0.5 rounded-full 
                                    @if($rec->based_on == 'daily_model') bg-purple-100 text-purple-700
                                    @elseif($rec->based_on == 'student_pattern') bg-blue-100 text-blue-700
                                    @else bg-gray-100 text-gray-700 @endif">
                                    {{ $rec->based_on == 'daily_model' ? 'Model' : ($rec->based_on == 'student_pattern' ? 'Pola' : 'Manual') }}
                                </span>
                            </div>
                            <p class="text-xs md:text-sm text-gray-500">{{ $rec->recommendation_date->format('d M Y') }}</p>
                        </div>
                    </div>
                    
                    <div class="flex items-center justify-between sm:justify-end space-x-4 ml-10 sm:ml-0">
                        <span class="text-xs md:text-sm text-gray-600">{{ $rec->recommended_minutes }} menit</span>
                        <a href="{{ route('siswa.recommendation.detail', $rec->id) }}" 
                           class="text-blue-600 hover:text-blue-800 text-xs md:text-sm font-medium">
                            Detail →
                        </a>
                    </div>
                </div>
                
                <!-- Preview notes untuk mobile -->
                <p class="text-xs text-gray-500 mt-2 line-clamp-1 sm:hidden">{{ $rec->notes }}</p>
            </div>
            @endforeach
        </div>
    </div>
    @endif
</div>
@endsection
