<?php

namespace App\Models;

use App\Mailer\DynamicMailConfig;
use App\Notifications\ResetPassword;
use App\Traits\Uuids;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Mail;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Swift_Mailer;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
    use HasRoles;
    use Uuids;

    public $incrementing = false;
    protected $keyType = 'uuid';

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'email_verified_at'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];

    public function sendEmailVerificationNotification()
    {
        $this->notifyEmailVerification(new \App\Notifications\EmailVerification());
    }

    public function notifyEmailVerification($instance)
    {
        $transport = DynamicMailConfig::getConfig();
        $swift_mailer = new Swift_Mailer($transport);

        Mail::setSwiftMailer($swift_mailer);
        Mail::send('email.email_verification', [
                'url' => $instance->getUrl($this),
            ], function ($message) {
                $message->from('loker@unesa.ac.id', 'Universitas Negeri Surabaya');
                $message->sender('loker@unesa.ac.id', 'Universitas Negeri Surabaya');
                $message->subject('Verify Email Address');
                $message->to($this->email, $this->email);
            });
    }

    // public function sendPasswordResetNotification($token)
    // {
    //     dd(new ResetPassword($token));
    //     $this->notify(new ResetPassword($token));
    // }
}