<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Concerns\GeneratesDberpCode;
use App\Models\Concerns\HasDberpAuditTrail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpSettlementAdjustment extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;

    protected $table = 'dberp_settlement_adjustments';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'settlement_side',
        'counterparty_type',
        'counterparty_id',
        'receivable_record_id',
        'payable_record_id',
        'adjustment_direction',
        'adjustment_type',
        'amount',
        'effective_amount',
        'status',
        'business_date',
        'handler_employee_id',
        'source_system',
        'external_document_no',
        'external_reference_id',
        'effective_at',
        'voided_at',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'counterparty_id' => 'integer',
            'receivable_record_id' => 'integer',
            'payable_record_id' => 'integer',
            'amount' => 'decimal:4',
            'effective_amount' => 'decimal:4',
            'business_date' => 'datetime',
            'handler_employee_id' => 'integer',
            'effective_at' => 'datetime',
            'voided_at' => 'datetime',
        ];
    }

    public function getDberpNumberScope(): string
    {
        return 'settlement_adjustment';
    }

    public function getDberpAuditActionPrefix(): string
    {
        return 'dberp.settlement_adjustment';
    }

    /**
     * @return list<string>
     */
    public function getDberpAuditFields(): array
    {
        return $this->getFillable();
    }

    public function handlerEmployee(): BelongsTo
    {
        return $this->belongsTo(DberpEmployee::class, 'handler_employee_id');
    }

    public function receivableRecord(): BelongsTo
    {
        return $this->belongsTo(DberpReceivableRecord::class, 'receivable_record_id');
    }

    public function payableRecord(): BelongsTo
    {
        return $this->belongsTo(DberpPayableRecord::class, 'payable_record_id');
    }
}