<?php



namespace App\Models\Settings;



use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

use App\Http\Controllers\Controller;

use Illuminate\Notifications\Notifiable;

use App\Models\Website\SiteUrlShortenerMapping;


class CmnBranch extends Model

{
    use Notifiable;

    protected $appends = ['is_shorten_link'];

    protected $fillable = [

        'id',

        'name',

        'name_slug',

        'branch_code',

        'short_name',

        'phone',

        'email',

        'address',

        'order',

        'status',

        'created_by',

        'updated_by'

    ];

    public function scopeUserBranches($query)

    {

        $br = new Controller();

        return $query->whereIn('cmn_branches.id', $br->getUserBranch()->pluck('cmn_branch_id'));
    }

    public function getDecodedNameAttribute()

    {
        $decodedName = htmlspecialchars_decode($this->name, ENT_QUOTES);
        
        return $decodedName;
    }

    public function siteUrlMapping()
    {
        return $this->hasOne(SiteUrlShortenerMapping::class, 'short_code', 'name_slug');
    }

    public function getIsShortenLinkAttribute()
    {
        return $this->siteUrlMapping ? 1 : 0;
    }
}
