<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;

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

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'nama',
        'username',
        'email',
        'password',
        'alamat',
        'no_telp',
        'tipe_pengguna',
        'role'
    ];

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

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'tipe_pengguna' => 'string'
    ];
    
    /**
     * Relasi ke model Sewa
     */
    public function sewas()
    {
        return $this->hasMany(Sewa::class);
    }

    /**
     * Relasi ke model Chat sebagai pengirim
     */
    public function sentChats()
    {
        return $this->hasMany(Chat::class, 'sender_id');
    }

    /**
     * Relasi ke model Chat untuk menghitung pesan yang belum dibaca
     */
    public function chats()
    {
        return $this->hasMany(Chat::class, 'sender_id');
    }

    /**
     * Mengecek apakah user adalah admin
     */
    public function isAdmin()
    {
        return $this->tipe_pengguna === 'admin';
    }
}
