<?php

namespace App\Models;

use App\Enums\DocumentStatus;
use App\Models\Concerns\HasPublicId;
use App\Models\Concerns\ImmutableWhenPosted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;

final class ContraSettlement extends Model
{
    use HasPublicId, ImmutableWhenPosted;

    protected $fillable = ['public_id','company_id','branch_id','party_id','contra_number','settlement_date','amount','customer_invoice_id','vendor_bill_id','receivable_account_id','payable_account_id','reason','status','approved_by','approved_at','posted_at','created_by'];
    protected function casts(): array { return ['settlement_date'=>'date','amount'=>'decimal:4','status'=>DocumentStatus::class,'approved_at'=>'datetime','posted_at'=>'datetime']; }
    public function company(): BelongsTo { return $this->belongsTo(Company::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function party(): BelongsTo { return $this->belongsTo(Party::class); }
    public function invoice(): BelongsTo { return $this->belongsTo(CustomerInvoice::class, 'customer_invoice_id'); }
    public function bill(): BelongsTo { return $this->belongsTo(VendorBill::class, 'vendor_bill_id'); }
    public function journal(): MorphOne { return $this->morphOne(JournalEntry::class, 'source'); }
}
