<?php

declare(strict_types=1);

namespace ImovelZapAi\Financial\Infrastructure\Persistence\Eloquent;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use ImovelZapAi\Financial\Domain\Enums\PaymentMethod;
use ImovelZapAi\Financial\Domain\Enums\PaymentStatus;
use ImovelZapAi\Sales\Infrastructure\Persistence\Eloquent\DealModel;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;

class FinancialPaymentModel extends Model
{
    use BelongsToTenant;
    use HasUuidPrimaryKey;

    protected $table = 'financial_payments';

    protected $fillable = [
        'tenant_id',
        'deal_id',
        'schedule_id',
        'transaction_id',
        'account_id',
        'method',
        'amount',
        'paid_at',
        'reference',
        'proof_path',
        'status',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'method' => PaymentMethod::class,
            'amount' => 'decimal:2',
            'paid_at' => 'datetime',
            'status' => PaymentStatus::class,
        ];
    }

    public function deal(): BelongsTo
    {
        return $this->belongsTo(DealModel::class, 'deal_id');
    }

    public function schedule(): BelongsTo
    {
        return $this->belongsTo(FinancialPaymentScheduleModel::class, 'schedule_id');
    }

    public function transaction(): BelongsTo
    {
        return $this->belongsTo(FinancialTransactionModel::class, 'transaction_id');
    }

    public function account(): BelongsTo
    {
        return $this->belongsTo(FinancialAccountModel::class, 'account_id');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function receipt(): HasOne
    {
        return $this->hasOne(FinancialReceiptModel::class, 'payment_id');
    }
}
