<?php

namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Notifications\CustomVerifyEmail;
use App\Notifications\CustomResetPassword;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model implements AuthenticatableContract, JWTSubject, MustVerifyEmail, CanResetPassword
{
    use HasFactory, Authenticatable, Notifiable, SoftDeletes;
    protected $table = 'users';

    protected $fillable = [
        'name',
        'email',
        'password',
        'id_role',
        'service_id',
        'status',
        'google_id',
        'email_verified_at'
    ];
    public function role()
    {
        return $this->belongsTo(UserType::class, 'id_role', 'id_role');
    }

    public function usersDetail()
    {
        return $this->hasOne(UsersDetail::class);
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    // Method untuk mengecek apakah email sudah diverifikasi
    public function hasVerifiedEmail()
    {
        return !is_null($this->email_verified_at);
    }

    // Method untuk menandai email sebagai terverifikasi
    public function markEmailAsVerified()
    {
        return $this->forceFill([
            'email_verified_at' => $this->freshTimestamp(),
        ])->save();
    }

    // Method untuk mendapatkan alamat email yang akan diverifikasi
    public function getEmailForVerification()
    {
        return $this->email;
    }

    public function getEmailForPasswordReset()
    {
        return $this->email;
    }
    // Method untuk mengirim notifikasi verifikasi email
    public function sendEmailVerificationNotification()
    {
        $this->notify(new CustomVerifyEmail);
    }
    public function sendPasswordResetNotification($token)
    {
        $this->notify(new CustomResetPassword($token)); 
    }
//     public function sendPasswordResetNotification($token)
// {
//     $url = env('FRONTEND_URL') . "/reset-password?token=" . $token;

//     $this->notify(new CustomResetPassword($url));
// }

    public function bookings()
    {
        return $this->hasMany(Booking::class, 'id_user');
    }

    public function payments()
    {
        return $this->hasMany(Payment::class, 'user_id');
    }

    public function rooms()
    {
        return $this->hasMany(Rooms::class, 'id_user');
    }
    
    

}
