<?php
namespace App\Models;

use App\Scopes\OrganisationScope;
use Auth;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Budget extends Model
{
    use HasFactory;

    protected $table = "compensation_budgets";

    protected $fillable = [
        'organization_id',
        'department_id',
        'name',
        'amount',
        'start_date',
        'end_date',
        'type',
        'notes',
    ];

    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            if (Auth::check()) {
                $model->organisation_id = Auth::user()->organisation_id;
            }
        });
    }

    public function organization()
    {
        return $this->belongsTo(Organisation::class);
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }
}
