<?php
// app/Http/Controllers/Siswa/RecommendationController.php

namespace App\Http\Controllers\Siswa;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Recommendation;
use Carbon\Carbon;

class RecommendationController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        
        // Rekomendasi hari ini
        $todayRecommendation = Recommendation::where('user_id', $user->id)
            ->whereDate('recommendation_date', Carbon::today())
            ->with('activityLog')
            ->first();

        // Riwayat rekomendasi 7 hari terakhir
        $weekRecommendations = Recommendation::where('user_id', $user->id)
            ->whereDate('recommendation_date', '>=', Carbon::today()->subDays(7))
            ->with('activityLog')
            ->orderBy('recommendation_date', 'desc')
            ->get();

        // Statistik 7 hari
        $stats = [
            'ringan' => Recommendation::where('user_id', $user->id)
                ->whereDate('recommendation_date', '>=', Carbon::today()->subDays(7))
                ->where('category', 'Ringan')
                ->count(),
            'sedang' => Recommendation::where('user_id', $user->id)
                ->whereDate('recommendation_date', '>=', Carbon::today()->subDays(7))
                ->where('category', 'Sedang')
                ->count(),
            'intensif' => Recommendation::where('user_id', $user->id)
                ->whereDate('recommendation_date', '>=', Carbon::today()->subDays(7))
                ->where('category', 'Intensif')
                ->count(),
        ];

        return view('siswa.recommendations', compact(
            'todayRecommendation',
            'weekRecommendations',
            'stats'
        ));
    }

    public function show($id)
    {
        $recommendation = Recommendation::where('user_id', Auth::id())
            ->with('activityLog')
            ->findOrFail($id);

        return view('siswa.recommendation-detail', compact('recommendation'));
    }
}
