<?php

namespace App\Repositories;

use App\Models\Reforme;

class ReformeRepository extends BaseRepository
{
    public function __construct(Reforme $model)
    {
        parent::__construct($model);
    }

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

    public function getActiveReformes()
    {
        return $this->query->where('date_debut', '<=', now())
            ->where(function ($query) {
                $query->where('date_fin', '>=', now())
                    ->orWhereNull('date_fin');
            })->get();
    }

    public function getStatistics()
    {
        return [
            'total' => $this->count(),
            'active' => $this->getActiveReformes()->count(),
            'by_year' => $this->query->selectRaw('YEAR(date_debut) as year, count(*) as count')
                ->groupBy('year')
                ->get()
                ->pluck('count', 'year'),
        ];
    }
}
