<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;

class User extends Authenticatable implements FilamentUser
{
    /** @use HasFactory<UserFactory> */
    use HasFactory, Notifiable, SoftDeletes;

    public function canAccessPanel(Panel $panel): bool
    {
        return true;
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'gender',
        'dob',
        'dob_code',
        'phone_number',
        'address',
        'email',
        'profile_photo_path',
        'is_alive',
        'date_of_death',
        'notes',
        'father_id',
        'mother_id',
        'role',
    ];

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

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'dob' => 'date',
            'date_of_death' => 'date',
            'is_alive' => 'boolean',
        ];
    }

    public function father()
    {
        return $this->belongsTo(User::class, 'father_id');
    }

    public function mother()
    {
        return $this->belongsTo(User::class, 'mother_id');
    }

    public function childrenAsFather()
    {
        return $this->hasMany(User::class, 'father_id');
    }

    public function childrenAsMother()
    {
        return $this->hasMany(User::class, 'mother_id');
    }

    public function getChildrenAttribute()
    {
        if ($this->gender === 'Lelaki') {
            return $this->childrenAsFather;
        } elseif ($this->gender === 'Perempuan') {
            return $this->childrenAsMother;
        }
        return $this->childrenAsFather->merge($this->childrenAsMother);
    }

    public function marriagesAsHusband()
    {
        return $this->hasMany(Marriage::class, 'husband_id');
    }

    public function marriagesAsWife()
    {
        return $this->hasMany(Marriage::class, 'wife_id');
    }

    public function galleries()
    {
        return $this->hasMany(Gallery::class);
    }
}
