<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ClientAddress extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id',
        'address_line_1',
        'city',
        'state',
        'is_primary',
        'notes',
    ];

    protected $casts = [
        'is_primary' => 'boolean',
    ];

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

    /**
     * Dirección legible para que el cobrador se ubique durante el recorrido:
     * "address_line_1, city, state" (omite segmentos vacíos).
     */
    protected function fullAddress(): Attribute
    {
        return Attribute::make(
            get: fn (): string => collect([$this->address_line_1, $this->city, $this->state])
                ->filter()
                ->implode(', '),
        );
    }
}
