<?php

namespace App\Repositories;

use App\Models\Infrastructure;

class InfrastructureRepository extends BaseRepository
{
    public function __construct(Infrastructure $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 findByTypeInfrastructure($typeInfrastructureId)
    {
        return $this->query->where('type_infrastructure_id', $typeInfrastructureId)->get();
    }

    public function getStatistics()
    {
        return [
            'total' => $this->count(),
            'by_type' => $this->query->select('type_infrastructure_id')
                ->selectRaw('count(*) as count')
                ->groupBy('type_infrastructure_id')
                ->get()
                ->pluck('count', 'type_infrastructure_id'),
            'by_annee' => $this->query->select('annee_id')
                ->selectRaw('count(*) as count')
                ->groupBy('annee_id')
                ->get()
                ->pluck('count', 'annee_id'),
        ];
    }
}
