<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class AuthorizationQueue extends Model
{
    use HasFactory;

    protected $table = 'authorization_queue';

    protected $fillable = [
        'uuid',
        'module',
        'action',
        'record_type',
        'record_id',
        'payload',
        'amount',
        'priority',
        'status',
        'inputer_id',
        'authorizer_id',
        'remarks',
        'decided_at',
    ];

    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (self $item): void {
            if (empty($item->uuid)) {
                $item->uuid = (string) Str::uuid();
            }
        });
    }

    protected function casts(): array
    {
        return [
            'payload' => 'array',
            'amount' => 'decimal:2',
            'decided_at' => 'datetime',
        ];
    }

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

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