<?php

namespace App\Models;

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

class Customer extends Model
{
    protected $fillable = [
        'organization_id',
        'mariachi_group_id',
        'name',
        'email',
        'mobile',
        'address_line_1',
        'address_line_2',
        'town',
        'state_province',
        'post_code',
        'country',
        'is_billing_same',
        'is_active',
    ];

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

    public function organization()
    {
        return $this->belongsTo(Organization::class);
    }

    public function mariachiGroup()
    {
        return $this->belongsTo(MariachiGroup::class);
    }

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

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