<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Support\Facades\Auth;

class Asset extends Model
{
    protected $fillable = [
        'employee_id',
        'name',
        'purchase_date',
        'supported_date',
        'amount',
        'description',
        'created_by',
        'organisation_id',

    ];

    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 users($users)
    {
        $userArr = explode(',', $users);
        $emp = Employee::whereIn('id', $userArr);
        $employees = $emp->get()->pluck('id');
        $users  = [];
        foreach ($employees as $user) {
            $emp = Employee::find($user);
            $users[] = User::where('id', $emp->user_id)->first();
        }

        return $users;
    }
}
