<?php

namespace App\Models;

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

class FunctionType extends Model
{
    use HasFactory, SoftDeletes;

    protected $primaryKey = 'Fnc_Id';

    protected $fillable = [
        'Act_Id',
        'gvn_no',
        'Mod_Code',
        'Fnc_Title',
        'Fnc_Desc',
        'GrCod',
        'UpGrp',
        'GrSrn',
        'GrStt',
        'isvisible',
        'Is_Quick_Action',
        'Description',
        'Menu_Links',
    ];

    protected $casts = [
        'Fnc_Title'    => 'string',
        'Fnc_Desc'     => 'string',
        'GrCod'        => 'string',
        'UpGrp'        => 'string',
        'GrSrn'        => 'string',
        'GrStt'        => 'string',
        'isvisible'    => 'integer',
        'Is_Quick_Action'    => 'integer',
        'Description'  => 'string',
        'Menu_Links'   => 'string',
        'Mod_Code'     => 'string',
    ];

    // Scope for active records
    public function scopeActive($query)
    {
        return $query->where('isvisible', 1);
    }

    // Scope for inactive records
    public function scopeInactive($query)
    {
        return $query->where('isvisible', 0);
    }

    public function action()
    {
        return $this->belongsTo(ActionMaster::class, 'Act_Id', 'Act_Id');
    }

    public function genVoucherNo()
    {
        return $this->belongsTo(GenVoucherNo::class, 'gvn_no', 'gvn_no');
    }

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

    public function getIsQuickActionTextAttribute()
    {
        return $this->Is_Quick_Action ? 'Yes' : 'No';
    }
}
