<?php

namespace App\Models;

use App\Casts\TrimmedDecimal;
use App\Concerns\RecordsCreator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable;

class SupplierPayment extends Model implements Auditable
{
    use AuditableTrait, RecordsCreator;

    /**
     * @var list<string>
     */
    protected $fillable = [
        'purchase_supplier_id',
        'payment_type',
        'purchase_id',
        'currency',
        'bill_currency',
        'exchange_rate',
        'amount',
        'bill_amount',
        'paid_at',
        'payment_method',
        'reference',
        'notes',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'purchase_supplier_id' => 'integer',
            'purchase_id' => 'integer',
            'amount' => TrimmedDecimal::class.':2',
            'bill_amount' => TrimmedDecimal::class.':2',
            'exchange_rate' => TrimmedDecimal::class.':4',
            'paid_at' => 'date',
        ];
    }

    /**
     * @return BelongsTo<PurchaseSupplier, $this>
     */
    public function supplier(): BelongsTo
    {
        return $this->belongsTo(PurchaseSupplier::class, 'purchase_supplier_id');
    }

    /**
     * @return HasMany<SupplierPaymentAllocation, $this>
     */
    public function allocations(): HasMany
    {
        return $this->hasMany(SupplierPaymentAllocation::class);
    }

    /**
     * @return BelongsTo<Purchase, $this>
     */
    public function purchase(): BelongsTo
    {
        return $this->belongsTo(Purchase::class);
    }
}
