<?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;
use Illuminate\Database\Eloquent\Relations\HasMany;

use App\Models\DberpSettlementWriteOffLine;

class DberpSettlementWriteOff extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;

    protected $table = 'dberp_settlement_write_offs';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'settlement_side',
        'counterparty_type',
        'counterparty_id',
        'total_amount',
        'reversed_amount',
        'effective_amount',
        'write_off_status',
        'business_date',
        'handler_employee_id',
        'source_system',
        'external_document_no',
        'external_reference_id',
        'confirmed_at',
        'reversed_at',
        'voided_at',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'counterparty_id' => 'integer',
            'total_amount' => 'decimal:4',
            'reversed_amount' => 'decimal:4',
            'effective_amount' => 'decimal:4',
            'business_date' => 'datetime',
            'handler_employee_id' => 'integer',
            'confirmed_at' => 'datetime',
            'reversed_at' => 'datetime',
            'voided_at' => 'datetime',
        ];
    }

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

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

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

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

    public function lines(): HasMany
    {
        return $this->hasMany(DberpSettlementWriteOffLine::class, 'write_off_id');
    }
}