<?php

namespace App\Models;

use App\Traits\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use App\Constants\Status;

class Transaction extends Model
{
    use Searchable;

    protected $casts = ['metadata' => 'json'];

    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function receiverUser()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    protected function statusBadge(): Attribute
    {
        return Attribute::make(function () {
            $html = '';
            switch ($this->status) {
                case Status::Pending:
                    $html = '<span class="badge badge--warning">' . trans('Pending') . '</span>';
                    break;
                case Status::Approved:
                    $html = '<span><span class="badge badge--success">' . trans('Approved') . '</span></span>';
                    break;
                case Status::Rejected:
                    $html = '<span><span class="badge badge--danger">' . trans('Rejected') . '</span></span>';
                    break;
            }
            return $html;
        });
    }

}
