<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Account extends Model
{
    use HasFactory;

    /** @use HasFactory<\Database\Factories\Person/AccountFactory> */
    use SoftDeletes;

    protected $fillable = [
        'reference',
        'description',
        'contract_id',
        'type_id',
        'active',
        'initial_amount',
        'balance_amount',
    ];

    protected function casts(): array
    {
        return [
            'initial_amount' => 'decimal:4',
            'balance_amount' => 'decimal:4',
            'promise_date' => 'date',
        ];
    }

    public function debtor(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Person\Person::class, 'debtor_id');
    }

    public function client(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Person\Person::class, 'client_id');
    }

    public function type(): BelongsTo
    {
        return $this->belongsTo(\App\Models\AccountType::class);
    }

    public function debts(): HasMany
    {
        return $this->hasMany(\App\Models\Debt::class);
    }

    public function payments(): HasMany
    {
        return $this->hasMany(\App\Models\Payment::class);
    }

    public function installments(): HasMany
    {
        return $this->hasMany(\App\Models\Installment::class);
    }

    public function bookings(): HasMany
    {
        return $this->hasMany(\App\Models\Booking::class);
    }

    public function contract(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Contract::class);
    }
}
