<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;

class Member extends Authenticatable
{
    use HasFactory, HasApiTokens;

    protected $table = 'member';

    protected $fillable = [
        'nik',
        'no_anggota',
        'password',
        'nama',
        'tempat_lahir',
        'tanggal_lahir',
        'jenis_kelamin',
        'gol_darah',
        'agama',
        'pekerjaan',
        'unit_kerja',
        'alamat',
        'email',
        'no_telp',
        'status',
        'level',
        'total_pokok',
        'total_mudarabah',
        'total_qurban',
        'total_umrah',
        'total_haji',
        'total_ijah',
        'total_wajib',
        'total_tabungan',
        'total_property',
        'create_user',
        'create_time',
        'update_user',
        'update_time',
    ];

    protected $hidden = [
        'password',
    ];

    protected $casts = [
        'tanggal_lahir' => 'date',
        'total_pokok' => 'decimal:0',
        'total_mudarabah' => 'decimal:0',
        'total_qurban' => 'decimal:0',
        'total_umrah' => 'decimal:0',
        'total_haji' => 'decimal:0',
        'total_ijah' => 'decimal:0',
        'total_wajib' => 'decimal:0',
        'total_tabungan' => 'decimal:0',
        'total_property' => 'decimal:0',
        'create_time' => 'datetime',
        'update_time' => 'datetime',
    ];

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';

    /**
     * Get the password for authentication
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    public function webUser()
    {
        return $this->hasOne(WebUser::class, 'user_id', 'nik');
    }

    public function simpanan()
    {
        return $this->hasMany(Simpanan::class, 'id_member');
    }

    public function pinjaman()
    {
        return $this->hasMany(Pinjaman::class, 'id_member');
    }
}
