<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

/**
 * @property $id
 * @property $effective_betting_amount
 * @property $date
 * @property $rebate_ratio
 * @property $member_id
 * @property $status
 * @property $amount
 * @property $audited_by
 * @property $grant_time
 */
class Rebate extends BaseModel
{
    protected $table = 'rebates';
    protected $fillable = ['date', 'member_id', 'betting_amount', 'rebate_ratio',
        'amount', 'status', 'first_name', 'username', 'audited_by',
        'profit', 'huishui_percentage', 'huishui_restriction', 'huishui_amount',
        'effective_betting_amount', 'grant_time'
    ];
    const STATUS_WEI_FAN_YONG = 0;//未返佣
    const STATUS_FAN_YONG = 1; //已返佣

    public function member()
    {
        return $this->belongsTo(User::class, 'member_id', 'member_id')
            ->select(['id', 'member_id', 'username', 'first_name', 'admin_note', 'status', 'phone', 'visitor_id', 'register_ip']);
    }


    static function updateProfit($data)
    {
        $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
        $rebate = static::where('date', $data['date'])
            ->where('member_id', $data['member_id'])->first();
        if ($rebate) {
            $profit = bcadd($rebate->profit, $data['profit'], 2);
            $rebate->profit = $profit;
            $rebate->save();
        }
        return $rebate;
    }

    static function addOrUpdate($data)
    {
        $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
        $data['rebate_ratio'] = Config::where('field', 'rebate')->first()->val;
        $data['huishui_restriction'] = Config::where('field', 'huishui_restriction')->first()->val;
        $data['huishui_percentage'] = Config::where('field', 'huishui_percentage')->first()->val;

        $rebate = static::where('date', $data['date'])
            ->where('member_id', $data['member_id'])->first();
        if ($rebate) {
            $rebate->rebate_ratio = $data['rebate_ratio'];
            $rebate->first_name = $data['first_name'];
            $rebate->username = $data['username'];
            $rebate->save();
            $rebate->increment("betting_amount", $data['betting_amount']);
        } else {
            $rebate = static::create($data);
        }
        return $rebate;
    }

    protected function getGrantTimeAttribute($value): string
    {
        if ($value > 0) return date('Y-m-d H:i', $value);
        return "";
    }

    protected function getHuishuiRestrictionAttribute($value)
    {
        if ($this->status == static::STATUS_WEI_FAN_YONG) {
            $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
            return $huishui_restriction;
        }
        return $value;

    }

    protected function getHuishuiPercentageAttribute($value)
    {
        if ($this->status == static::STATUS_WEI_FAN_YONG) {
            $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
            return $huishui_percentage;
        }
        return $value;
    }

    protected function getHuishuiAmountAttribute($value)
    {
        if ($this->status == static::STATUS_WEI_FAN_YONG) {
            $value = 0;
            $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
            $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
            $lose = $this->profit * -1;
            if ($lose >= $huishui_restriction) {
                $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
            }
        }
        return $value;
    }
}