<?php

namespace App\Models;

use App\Traits\HasCacheModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class Client extends Model
{
    use HasFactory;
    use HasCacheModel;

    const DEFAULT_PASSWORD = "Pay@2023";
    const PATH_AVATAR = 'client-avatar';

    public function __construct()
    {
        $this->table = 'users';
        $this->guarded = [
            'password',
            'email',
            'remember_token'
        ];
        $this->hidden = [
            'password',
            'remember_token'
        ];
        $this->append([
            'country_name',
            'country_flag'
        ]);
    }

    // custom attribute get{name}Attribute()
    public function getAvatarAttribute($path)
    {
        if ($path) {
            if (Str::isUrl($path)) {
                return $path;
            } else {
                return Storage::disk('public')->url($path);
            }
        }
        return asset('img/avatar.png');
    }

    public function getCountryNameAttribute()
    {
        $countries = HCountry::getByCache();

        return $countries
            ->where('code', $this->country_code)
            ->value('name');
    }

    public function getCountryFlagAttribute()
    {
        $code = $this->country_code;
        return asset(HCountry::PUBLIC_PATH . "/$code.svg");
    }
}
