<?php

namespace App\Models;

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

class Reward extends Model
{
    use HasFactory, Searchable;

    protected $guarded = [];
    protected $casts = [
        'status_changed_at' => 'datetime'
    ];

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

    protected function statusBadge(): Attribute
    {
        return Attribute::make(function () {
            $html = '';
            switch ($this->status) {
                case Status::Approved:
                    $html = '<span class="badge badge--success">' . trans('Approved') . '</span>';
                    break;
                case Status::Pending:
                    $html = '<span class="badge badge--warning">' . trans('Pending') . '</span>';
                    break;
                case Status::Rejected:
                    $html = '<span class="badge badge--danger">' . trans('Rejected') . '</span>';
                    break;
            }
            return $html;
        });
    }
    protected function rankBadge(): Attribute
    {
        return Attribute::make(function () {
            $html = '';
            switch ($this->rank) {
                case 1:
                    $html = '<span class="badge badge--primary">' . trans('Rank 1') . '</span>';
                    break;
                case 2:
                    $html = '<span class="badge badge--secondary">' . trans('Rank 2') . '</span>';
                    break;
                case 3:
                    $html = '<span class="badge badge--success">' . trans('Rank 3') . '</span>';
                    break;
                case 4:
                    $html = '<span class="badge badge--info">' . trans('Rank 4') . '</span>';
                    break;
                case 5:
                    $html = '<span class="badge badge--warning">' . trans('Rank 5') . '</span>';
                    break;
                case 6:
                    $html = '<span class="badge badge--danger">' . trans('Rank 6') . '</span>';
                    break;
                case 7:
                    $html = '<span class="badge badge--dark">' . trans('Rank 7') . '</span>';
                    break;
            }
            return $html;
        });
    }

    protected function winStatus(): Attribute
    {
        return Attribute::make(function () {
            if ($this->status_changed_at && $this->status == Status::Rejected) {
                return '<span class="badge badge--danger">' . trans('Rejected') . '</span>';
            }
            return '<span class="badge badge--success">' . trans('Achieved') . '</span>';
        });
    }

    public function transaction()
    {
        return $this->hasOne(Transaction::class, 'trx', 'trx');
    }
}
