<?php

namespace Modules\MedBilling\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * A payer master row (SIFA P4-T1, audit decision D8). Payers are a dedicated
 * table — NOT ERP contacts — and may carry their own ERP selling_price_groups
 * price list via default_price_group_id.
 */
class MedbillPayer extends Model
{
    protected $table = 'medbill_payers';

    protected $guarded = ['id'];

    protected $casts = [
        'contact'                   => 'array',
        'pre_auth_required_default' => 'boolean',
        'is_active'                 => 'boolean',
        'settlement_terms_days'     => 'integer',
    ];

    public const PAYER_TYPES       = ['insurance', 'corporate', 'government', 'other'];
    public const CLAIM_SUBMISSIONS = ['portal', 'email', 'edi', 'paper'];

    public function policies()
    {
        return $this->hasMany(MedbillPatientPolicy::class, 'payer_id');
    }

    public function claims()
    {
        return $this->hasMany(MedbillClaim::class, 'payer_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }
}
