<?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 Laravolt\Indonesia\Models\Province;
use Laravolt\Indonesia\Models\City;
use Laravolt\Indonesia\Models\District;
use Laravolt\Indonesia\Models\Village;

/**
 * Mendefinisikan properti agar Intelephense/VS Code tidak error
 * @property string $role
 * @property int $province_id
 * @property int $regency_id
 * @property int $district_id
 * @property int $village_id
 */
class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;

    /**
     * Atribut yang dapat diisi secara massal.
     */
    protected $fillable = [
    'name', 
    'email', 
    'password', 
    'role',
    'province_id', 
    'regency_id', 
    'district_id', 
    'village_id', 
    'postal_code', 
    'address', 
    'lat', 
    'lng'

    ];

    /**
     * Atribut yang harus disembunyikan.
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Casting atribut (Laravel 11/12 style).
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    /*
    |--------------------------------------------------------------------------
    | Relasi Wilayah Indonesia (Laravolt)
    |--------------------------------------------------------------------------
    */

    public function province()
    {
        return $this->belongsTo(Province::class, 'province_id');
    }

    public function regency()
    {
        return $this->belongsTo(City::class, 'regency_id');
    }

    public function district()
    {
        return $this->belongsTo(District::class, 'district_id');
    }

    public function village()
    {
        return $this->belongsTo(Village::class, 'village_id');
    }

    /*
    |--------------------------------------------------------------------------
    | Helper Functions
    |--------------------------------------------------------------------------
    */

    /**
     * Cek apakah user adalah admin
     */
    public function isAdmin(): bool
{
    return $this->role === 'admin';
}


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


public function orders()
{
    return $this->hasMany(Order::class);
}



}   