<?php

namespace Modules\MedOrganization\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * User ↔ department assignment (m2m + is_primary). Backs the department
 * data-scope (OrgScope::departmentIds()).
 */
class OrgUserDepartment extends Model
{
    protected $table = 'org_user_departments';

    protected $guarded = ['id'];

    protected $casts = [
        'is_primary' => 'boolean',
    ];

    public function user()
    {
        return $this->belongsTo(\App\User::class, 'user_id');
    }

    public function department()
    {
        return $this->belongsTo(\Modules\ClinicCore\Entities\ClcDepartment::class, 'department_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }
}
