<?php

namespace App\Models;

use App\Traits\Uuid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Laravel\Scout\Searchable;

class ClientCase extends Model
{
    /** @use HasFactory<\Database\Factories\ClientCaseFactory> */
    use HasFactory;

    use Searchable;
    use Uuid;

    protected $fillable = [
        'client_id',
        'person_id',
        'client_ref',
    ];

    protected $hidden = [
        'id',
        'client_id',
        'person_id',
    ];

    protected function makeAllSearchableUsing(Builder $query): Builder
    {
        return $query->with('person');
    }

    public function toSearchableArray(): array
    {

        return [
            'person.full_name' => '',
        ];
    }

    public function client(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Client::class);
    }

    public function person(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Person\Person::class)
            ->with(['phones', 'addresses']);
    }

    public function contracts(): HasMany
    {
        return $this->hasMany(\App\Models\Contract::class);
    }

    public function activities(): HasMany
    {
        return $this->hasMany(\App\Models\Activity::class);
    }

    public function segments(): BelongsToMany
    {
        return $this->belongsToMany(\App\Models\Segment::class)->withTimestamps();
    }

    public function documents(): MorphMany
    {
        return $this->morphMany(\App\Models\Document::class, 'documentable');
    }
}
