<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Customers\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * @property-read Customer $customer
 */
class Address extends Model
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'customer_id',
        'type',
        'street',
        'house_number',
        'house_number_addition',
        'postal_code',
        'city',
        'country',
        'is_primary',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_primary' => 'boolean',
        ];
    }

    /**
     * @return BelongsTo<Customer, Address>
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    public function getFullAddressAttribute(): string
    {
        $line1 = trim("{$this->street} {$this->house_number}");
        $addition = trim((string) $this->house_number_addition);
        if ($addition !== '') {
            $line1 .= ' '.$addition;
        }

        return trim("{$line1}, {$this->postal_code} {$this->city}, {$this->country}");
    }
}
