<?php

namespace App\Models\INSURANCE;

use App\Enums\AddressType;
use App\Models\CRM\Address;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Insurer extends Model
{
    /** @use HasFactory<\Database\Factories\INSURANCE\InsurerFactory> */
    use HasFactory;

    protected $fillable = [
        'business_name',
        'short_name',
        'mb',
        'pib',
        'email',
        'phone',
        'mobile',
        'status',
    ];

    public function addresses()
    {
        return $this->morphMany(Address::class, 'addressable');
    }

    public function lastActiveResidence()
    {
        return $this->morphOne(Address::class, 'addressable')
            ->where('type', AddressType::Residence->value)
            ->active()
            ->latest();
    }

    public function commissionDefaults()
    {
        return $this->hasMany(CommissionDefault::class, 'insurer_id');
    }

    public function insurancePolicies()
    {
        return $this->hasMany(InsurancePolicy::class);
    }

    public function contacts(): HasMany
    {
        return $this->hasMany(Contact::class);
    }
}
