<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Department;

class DesignationMaster extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'designation_master';
    protected $primaryKey = 'Dsg_id';

    protected $fillable = [
        'Dsg_Title',
        'Dsg_Desc',
        'Dsg_EName',
        'Dpt_id',
        'UpGrp',
        'GrCod',
        'GrSrn',
        'GrStt',
        'isvisible',
    ];

    protected $casts = [
        'Dpt_id' => 'array',
        'isvisible' => 'integer',
    ];

protected $appends = ['departments'];

    public function scopeActive($query)
    {
        return $query->where('isvisible', 1);
    }

    public function scopeInactive($query)
    {
        return $query->where('isvisible', 0);
    }

    public function getIsVisibleAttribute()
    {
        return $this->attributes['isvisible'] == 1 ? 'Active' : 'Inactive';
    }

public function getDepartmentsAttribute()
{
    $deptIds = $this->Dpt_id;

    if (!$deptIds) {
        return [];
    }

    if (!is_array($deptIds)) {
        $deptIds = [$deptIds];
    }

    return \App\Models\Department::whereIn('Dpt_id', $deptIds)->get();
}
}
