<?php

namespace App\Models;

use App\Enums\PartyRoleType;
use App\Models\Concerns\HasPublicId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class Party extends Model
{
    use HasPublicId;

    protected $fillable = ['public_id','company_id','assigned_branch_id','assigned_user_id','legal_name','trade_name','entity_type','trn','trade_license_number','license_expiry_date','identification_number','contact_person','phone','email','billing_address','operational_address','emirate','country_code','credit_limit','credit_period_days','default_currency','default_tax_code_id','default_income_account_id','default_expense_account_id','default_receivable_account_id','default_payable_account_id','einvoice_address','einvoice_scheme','notes','is_active'];
    protected function casts(): array { return ['license_expiry_date'=>'date','credit_limit'=>'decimal:4','is_active'=>'boolean']; }
    public function company(): BelongsTo { return $this->belongsTo(Company::class); }
    public function roles(): HasMany { return $this->hasMany(PartyRole::class); }
    public function hasRole(PartyRoleType $role): bool { return $this->roles()->where('role', $role->value)->exists(); }
}
