<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Support\PhoneNumber;

class ExternalPartner extends Model
{
    protected $fillable = [
        'organization_id',
        'name',
        'contact_name',
        'phone',
        'email',
        'city',
        'notes',
        'sort_order',
        'is_active',
    ];

    protected $casts = [
        'organization_id' => 'integer',
        'sort_order' => 'integer',
        'is_active' => 'boolean',
    ];

    public function serenatas()
    {
        return $this->hasMany(Serenata::class);
    }

    public function movements()
    {
        return $this->hasMany(ExternalPartnerMovement::class);
    }

    public function getFormattedPhoneAttribute(): string
    {
        return PhoneNumber::display($this->phone);
    }

    public function setPhoneAttribute($value): void
    {
        $this->attributes['phone'] = PhoneNumber::normalize(is_scalar($value) ? (string) $value : '') ?: null;
    }
}
