<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

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

/**
 * Class PaymentMethod
 *
 * @property int $id
 * @property string $name
 * @property string $short
 * @property int $show_at
 * @property int $pos
 * @property bool $active
 * @property bool $default
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @package App\Models
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereDefault($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereShort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereShowAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PaymentMethod whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $max_price
 * @method static \Illuminate\Database\Eloquent\Builder|PaymentMethod whereMaxPrice($value)
 */
class PaymentMethod extends Model
{

	protected $table = 'payment_methods';

	protected $casts = [
		'show_at' => 'int',
		'pos' => 'int',
		'active' => 'bool',
        'default' => 'bool',
	];

	protected $fillable = [
		'name',
        'short',
		'show_at',
        'max_price',
		'pos',
        'default',
		'active'
	];

    public static $showATs = [
        0 => 'Nur Kunden Shop',
        1 => 'Nur Vertriebspartner Shop',
        2 => 'Kunden + Vertriebspartner Shop',
        3 => 'Nur Reg/Mitgliedschaft Vertriebspartner',
        4 => 'Kunden + Vertriebspartner Shop + Reg/Mitgliedschaft',
        5 => 'Vertriebspartner Shop + Reg/Mitgliedschaft',
        9 => 'überall',
    ];

    public function getShowAtType(){
        return isset(self::$showATs[$this->show_at]) ? self::$showATs[$this->show_at] : '-';
    }

    public static function getDefaultAsArray($short=false){
        return PaymentMethod::where('active', true)->where('default', true)->pluck('id');
    }

    public function setMaxPriceAttribute($value)
    {
        $this->attributes['max_price'] = $value ? Util::reFormatNumber($value) : null;
    }

    public function getFormattedMaxPrice()
    {
        return isset($this->attributes['max_price']) ? Util::formatNumber($this->attributes['max_price']) : "";
    }

    /**
     * @param $short //PP FNC VOR CC SB ...
     * @param $user_payment_methods //PaymentMethod [] IDs
     * @param $total //Full price
     */
    public static function isShowPaymentMethod($short, $user_payment_methods, $total = 0){

        $payment_method = PaymentMethod::whereShort($short)->first();

        if($payment_method && $payment_method->active){
            if(in_array($payment_method->id, $user_payment_methods)){
                if($payment_method->max_price > 0){
                    if($payment_method->max_price >= $total){
                        return true;
                    }
                }else{
                    return true;
                }
            }
        }
        return false;
    }
}
