<?php

namespace App\Models;

use App\Http\Traits\LogsActivityCustom;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Laravel\Sanctum\HasApiTokens;
use App\Notifications\VerifyEmail;
use Spatie\Activitylog\LogOptions;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Http\Traits\Timezone;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles, Timezone;
    protected $casts = [
        'created_at' => 'datetime:d-m-Y H:i:s'
    ];
    protected static $recordEvents = ['deleted', 'created'];
    public $pageName = "User";
    public $colName = "name";
    public $guardName = 'web';

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'external_id',
        'last_login_at',
    ];

    public function setting()
    {
        return $this->hasOne(UserSetting::class, 'user_id','id');
    }

    public function image()
    {
        return $this->morphOne(Image::class, 'imageable');
    }

    public function sendPasswordResetNotification($token)
    {
        $details['name'] = $this->name;
        $details['url'] = route('member.password.reset', [$token, 'email' => $this->email]);
        $this->notify(new ResetPasswordNotification($details));
    }

    public function whereRoleAgent()
    {
        return $this->with('roles');
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    public function Web3PrivateKey(): Attribute
    {
        return Attribute::make(
            function ($value) {
                return substr($value, 2);
            }
        );
    }

    public function userAddress()
    {
        return $this->hasMany(\App\Models\CSRNow\UserAddress::class,'user_id','external_id');
    }

    public function details()
    {
        return $this->hasOne(\App\Models\CSRNow\User::class,'id','external_id');
    }

    public function member()
    {
        return $this->hasOne(Member::class,'user_id','id');
    }
}
