<?php

namespace Beike\Models;

use Beike\Notifications\ForgottenNotification;
use Beike\Notifications\RegistrationNotification;
use Beike\Notifications\RegistrationAlertNotification;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class Customer extends AuthUser implements JWTSubject
{
    use HasFactory;
    use Notifiable;
    use SoftDeletes;

    public const AUTH_GUARD = 'web_shop';

    public const STATUSES = [
        'pending',
        'approved',
        'rejected',
    ];

    protected $fillable = ['name', 'email', 'password', 'status', 'avatar', 'customer_group_id', 'locale', 'active', 'from', 'last_password_changed_at'];

    protected function serializeDate(\DateTimeInterface $date): string
    {
        return Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d H:i:s');
    }

    public function addresses(): HasMany
    {
        return $this->hasMany(Address::class);
    }

    public function customerGroup(): BelongsTo
    {
        return $this->belongsTo(CustomerGroup::class);
    }

    public function wishlists(): HasMany
    {
        return $this->hasMany(CustomerWishlist::class);
    }

    public function rmas(): HasMany
    {
        return $this->hasMany(Rma::class);
    }

    public function notifyRegistration()
    {
        $useQueue = system_setting('base.use_queue', true);
        if ($useQueue) {
            $this->notify(new RegistrationNotification($this));
            $this->notify(new RegistrationAlertNotification($this));
        } else {
            $this->notifyNow(new RegistrationNotification($this));
            $this->notifyNow(new RegistrationAlertNotification($this));
        }
    }

    public function notifyVerifyCodeForForgotten($code)
    {
        $useQueue = system_setting('base.use_queue', true);
        if ($useQueue) {
            $this->notify(new ForgottenNotification($this, $code));
        } else {
            $this->notifyNow(new ForgottenNotification($this, $code));
        }
    }

    /**
     * @param string $password
     * @return bool
     */
    public function matchPassword(string $password): bool
    {
        return password_verify($password, $this->password);
    }

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}
