<?php

namespace App\Repositories;

use App\Models\StructurePedagogique;

class StructurePedagogiqueRepository extends BaseRepository
{
    public function __construct(StructurePedagogique $model)
    {
        parent::__construct($model);
    }

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

    public function findByAnnee($anneeId)
    {
        return $this->query->where('annee_id', $anneeId)->get();
    }

    public function findByNiveau($niveauId)
    {
        return $this->query->where('niveau_id', $niveauId)->get();
    }

    public function getStatistics()
    {
        return [
            'total' => $this->count(),
            'total_eleves' => $this->query->sum('nombre_eleves'),
            'total_filles' => $this->query->sum('nombre_filles'),
            'by_niveau' => $this->query->select('niveau_id')
                ->selectRaw('count(*) as count')
                ->groupBy('niveau_id')
                ->get()
                ->pluck('count', 'niveau_id'),
            'by_annee' => $this->query->select('annee_id')
                ->selectRaw('count(*) as count')
                ->groupBy('annee_id')
                ->get()
                ->pluck('count', 'annee_id'),
        ];
    }
}
