<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php

namespace App\Services;

use App\Models\StudentRank;
use Illuminate\Support\Facades\DB;

class RankService
{
    /**
     * Calculate and save ranks for all students in a class
     */
    public function calculateAndSaveRanks($classId, $session, $sequence, $langType = null)
    {
        // Récupérer les données calculées AVEC le filtre de langue
        $studentsData = getStudentsWithRankAll($classId, $session, $sequence, $langType);
        
        DB::transaction(function() use ($studentsData, $classId, $session, $sequence, $langType) {
            foreach ($studentsData as $studentData) {
                $data = [
                    'total_points' => $studentData['totalPoints'],
                    'total_bareme' => $studentData['totalBareme'],
                    'average' => $studentData['moyenneGenerale'],
                    'rank' => $studentData['rank'],
                    'appreciation' => $this->getAppreciation($studentData['moyenneGenerale'])
                ];
                
                // CAS BILINGUE - avec lang_type_rank
                if ($langType) {
                    StudentRank::updateOrCreate(
                        [
                            'usr_id' => $studentData['student']->usr_id,
                            'class_id' => $classId,
                            'session' => $session,
                            'sequence' => $sequence,
                            'lang_type_rank' => $langType
                        ],
                        array_merge($data, ['lang_type_rank' => $langType])
                    );
                } 
                // CAS UNILINGUE - sans lang_type_rank
                else {
                    StudentRank::updateOrCreate(
                        [
                            'usr_id' => $studentData['student']->usr_id,
                            'class_id' => $classId,
                            'session' => $session,
                            'sequence' => $sequence
                        ],
                        $data
                    );
                }
            }
        });
        
        return $studentsData;
    }

    public function calculateAndSaveRanksOld($classId, $session, $sequence, $langType = null)
    {
        // Récupérer les données calculées
        $studentsData = getStudentsWithRankAll($classId, $session, $sequence);
        
        DB::transaction(function() use ($studentsData, $classId, $session, $sequence, $langType) {
            foreach ($studentsData as $studentData) {
                $data = [
                    'total_points' => $studentData['totalPoints'],
                    'total_bareme' => $studentData['totalBareme'],
                    'average' => $studentData['moyenneGenerale'],
                    'rank' => $studentData['rank'],
                    'appreciation' => $this->getAppreciation($studentData['moyenneGenerale'])
                ];
                
                // CAS BILINGUE - avec lang_type_rank
                if ($langType) {
                    StudentRank::updateOrCreate(
                        [
                            'usr_id' => $studentData['student']->usr_id,
                            'class_id' => $classId,
                            'session' => $session,
                            'sequence' => $sequence,
                            'lang_type_rank' => $langType // Inclut dans la clé unique
                        ],
                        array_merge($data, ['lang_type_rank' => $langType])
                    );
                } 
                // CAS UNILINGUE - sans lang_type_rank
                else {
                    StudentRank::updateOrCreate(
                        [
                            'usr_id' => $studentData['student']->usr_id,
                            'class_id' => $classId,
                            'session' => $session,
                            'sequence' => $sequence
                        ],
                        $data
                    );
                }
            }
        });
        
        return $studentsData;
    }

    /**
     * Get student rank
     */
    public function getStudentRank($studentId, $classId, $session, $sequence)
    {
        return StudentRank::where('usr_id', $studentId)
            ->where('class_id', $classId)
            ->where('session', $session)
            ->where('sequence', $sequence)
            ->first();
    }

    /**
     * Get class ranking
     */
    public function getClassRanking($classId, $session, $sequence)
    {
        return StudentRank::where('class_id', $classId)
            ->where('session', $session)
            ->where('sequence', $sequence)
            ->with('student')
            ->orderBy('rank', 'asc')
            ->get();
    }

    /**
     * Delete ranks for a specific period
     */
    public function deleteRanks($classId, $session, $sequence)
    {
        return StudentRank::where('class_id', $classId)
            ->where('session', $session)
            ->where('sequence', $sequence)
            ->delete();
    }

    /**
     * Get appreciation based on average
     */
    private function getAppreciation($moyenne)
    {
