<?php

namespace App\Models;

use App\Models\Settings\RegionalSettings\City;
use App\Models\Settings\RegionalSettings\Region;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasRoles, SoftDeletes;

    protected $table = 'users';
    protected $fillable = [
        'name',
        'username',
        'email',
        'mobile',
        'role',
        'avatar',
        'avatar_path',
        'gender',
        'birth_date',
        'address',
        'region_id',
        'city_id',
        'password',
        'email_verified_at',
        'mobile_verified_at',
        'password_changed_at',
        'is_active',
        'locale',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'mobile_verified_at' => 'datetime',
            'password_changed_at' => 'datetime',
            'password' => 'hashed',
            'birth_date' => 'date',
            'is_active' => 'boolean',
            'deleted_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::deleting(function ($user) {
            deleteAttachment($user->avatar, 'users-avatars');
        });
    }

    public function customers(): HasMany
    {
        return $this->hasMany(Customer::class, 'assigned_coach_id');
    }
    public function regions(): BelongsTo
    {
        return $this->belongsTo(Region::class, 'region_id');
    }
    public function cities(): BelongsTo
    {
        return $this->belongsTo(City::class, 'city_id');
    }
    public function register(): BelongsTo
    {
        return $this->belongsTo(__CLASS__, 'created_by');
    }
    public function updater(): BelongsTo
    {
        return $this->belongsTo(__CLASS__, 'updated_by');
    }
    public function deleter(): BelongsTo
    {
        return $this->belongsTo(__CLASS__, 'deleted_by');
    }


}
