<?php

namespace App\Models;

use App\Enums\MemberStatusEnum;
use App\Traits\FilterableTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Dependent extends Model
{
    use HasFactory, FilterableTrait;

    protected array $filterable = ['status', 'assigned_member_id'];
    protected array $searchable = ['name', 'phone'];
    protected array $sortable = ['id', 'name', 'created_at'];

    protected $fillable = [
        'name',
        'relation',
        'dob',
        'phone',
        'status',
        'assigned_member_id',
    ];

    protected function casts(): array
    {
        return [
            'relation' => 'integer',
            'dob'      => 'date',
            'status'   => 'integer',
        ];
    }

    public function member()
    {
        return $this->belongsTo(Member::class, 'assigned_member_id');
    }

    public function academies()
    {
        return $this->belongsToMany(Academy::class, 'academy_dependent')->withTimestamps();
    }

    public function scopeForIndex($query)
    {
        return $query->select(['id', 'name', 'relation', 'status', 'assigned_member_id', 'phone', 'created_at']);
    }

    public function scopeForShow($query)
    {
        return $query->with(['member']);
    }

    public function scopeStatusPending($query)
    {
        return $query->where('status', MemberStatusEnum::PENDING->value);
    }
}
