<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use App\Services\Util;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class UserCreditMargin
 *
 * @property int $id
 * @property int $user_id
 * @property float|null $credit
 * @property string|null $message
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property User $user
 * @package App\Models
 * @property bool|null $paid
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereCredit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereMessage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin wherePaid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereUserId($value)
 * @property int|null $user_credit_id
 * @method static \Illuminate\Database\Eloquent\Builder|UserCreditMargin whereUserCreditId($value)
 * @mixin \Eloquent
 */
class UserCreditMargin extends Model
{

	public $statusType = [
		1 => 'manually added margin',
	];

	protected $table = 'user_credit_margins';

	protected $casts = [
		'user_id' => 'int',
		'credit' => 'float',
		'status' => 'int',
		'paid' => 'bool',
		'user_credit_id' => 'int',
	];

	protected $fillable = [
		'user_id',
		'credit',
		'message',
		'status',
		'paid',
		'user_credit_id',
	];

	public function user()
	{
        return $this->belongsTo('App\User','user_id');
	}

	public function deleteTime(){
		$time = '+100 min';
		if(Carbon::parse($this->created_at)->modify($time)->gt(Carbon::now())){
			return Carbon::now()->diffInMinutes(Carbon::parse($this->created_at)->modify($time));
		}
		return false;
	}

	public function getFormattedCredit(){
		return Util::formatNumber($this->credit);
	}
}
