<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class CommentairesRapportActivite extends Model
{
    use HasFactory;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'commentaires_rapport_activite';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'titre',
        'date_paat',
        'periode_reference',
        'commentaires',
        'date_rapport',
        'user_id',
        'fichier_id',
    ];

    /**
     * Permet de retourner l'utilisateur qui a renseigné les commentaires du rapport
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Permet de retourner le rapport PDF lié s'il existe
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function fichier()
    {
        return $this->belongsTo(Fichier::class);
    }


    public static function get_current_monthly_rapport_activite()
    {
        return CommentairesRapportActivite::whereMonth('date_rapport', Carbon::now()->month)
            ->whereYear('date_rapport', Carbon::now()->year)
            ->where('user_id', '=', Auth::user()->id)
            ->first();
    }
}
