<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ManagementYear extends Model
{
    use HasFactory;

    protected $fillable = [
        'year',
        'start_date',
        'end_date',
        'status',
        'description'
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

    public function contributions()
    {
        return $this->hasMany(Contribution::class);
    }

    public function expectedContributions()
    {
        return $this->hasMany(ExpectedContribution::class);
    }

    /**
     * Trouve l'année de gestion pour une date donnée
     */
    public static function getForDate($date)
    {
        return self::where('start_date', '<=', $date)
            ->where('end_date', '>=', $date)
            ->first();
    }
}
