<?php

namespace App\Repositories;

use App\Models\Professeur;

class ProfesseurRepository extends BaseRepository
{
    public function __construct(Professeur $model)
    {
        parent::__construct($model);
    }

    public function findByMatricule($matricule)
    {
        return $this->query->where('matricule', $matricule)->first();
    }

    public function findByEtablissement($etablissementId)
    {
        return $this->query->where('etablissement_id', $etablissementId)->get();
    }

    public function findByDiscipline($disciplineId)
    {
        return $this->query->where('discipline_id', $disciplineId)->get();
    }

    public function search($term)
    {
        return $this->query->where(function ($query) use ($term) {
            $query->where('nom', 'like', "%{$term}%")
                  ->orWhere('prenom', 'like', "%{$term}%")
                  ->orWhere('matricule', 'like', "%{$term}%")
                  ->orWhere('email', 'like', "%{$term}%");
        })->get();
    }

    public function getStatistics()
    {
        return [
            'total' => $this->count(),
            'by_statut' => $this->query->select('statut_id')
                ->selectRaw('count(*) as count')
                ->groupBy('statut_id')
                ->get()
                ->pluck('count', 'statut_id'),
            'by_cycle' => $this->query->select('cycle_id')
                ->selectRaw('count(*) as count')
                ->groupBy('cycle_id')
                ->get()
                ->pluck('count', 'cycle_id'),
        ];
    }
}
