<?php

namespace Modules\Main\Models\Company;

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

use Modules\User\Models\User;
use Modules\User\Models\UserRole;
use Modules\User\Models\Role;

class CompanyAdmin extends Model
{
    use HasFactory;
    protected $fillable = [
        'user_id',
        'company_id'
    ];
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    protected static function booted(): void
    {
        static::created(function (CompanyAdmin $admin)
        {
            UserRole::create([
                'user_id' => $admin->user_id,
                'role_id' => Role::COMPANY_ADMIN
            ]);
        });
        static::deleted(function (CompanyAdmin $admin)
        {
            UserRole::where([
                'user_id' => $admin->user_id,
                'role_id' => Role::COMPANY_ADMIN
            ])->delete();
        });

    }
}
