<?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 Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Concerns\HasUuids;

class User extends Authenticatable implements JWTSubject
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'nama_depan',
        'nama_belakang',
        'tanggal_lahir',
        'nik',
        'email',
        'password',
        'role',
        'nohp',
        'alamat',
        'foto_ktp',
        'foto_wajah',
        'foto_profile',
        'persentase_kemiripan',
        'status',
        'gender',
        'kode_kelurahan',
        'remember_token',
    ];

    /**
     * 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',
        'id' => 'string',
        'status' => 'string',
    ];

    //JWT

    /**
     * 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 [];
    }

    //JWT


    //Relasi
    public function pemilik_kontak(){
        return $this->hasMany(Contact::class, 'email', 'pemilik_kontak');
    }

    public function relasi_kontak(){
        return $this->hasMany(Contact::class, 'email', 'relasi_kontak');
    }

    public function pembeli(){
        return $this->hasMany(Transaction::class, 'email', 'pembeli');
    }

    public function penjual(){
        return $this->hasMany(Transaction::class, 'email', 'penjual');
    }

    public function village(){
        return $this->belongsTo('Laravolt\Indonesia\Models\Village', 'kode_kelurahan', 'code');
    }
    //Relasi

    //function alamat
    public function getVillageName(){
        return $this->village->name;
    }

    public function getDistrictName(){
        return $this->village->district->name;
    }

    public function getCityName(){
        return $this->village->district->city->name;
    }

    public function getProvinceName(){
        return $this->village->district->city->province->name;
    }

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

}