<?php

namespace Server\Database\Models;

use Server\Database\Models\Traits\MiningTrait;
use Illuminate\Database\Eloquent\Model;

class User extends ApiModel 
{

    use MiningTrait;

    public $apiSearchBy = "first_name";

    protected $casts = [
        'traders' => 'object',
    ];

    protected $fillable = [
        'dob',
        'pin',
        'city',
        'state',
        'email',
        'user_id',
        'message',
        'country',
        'currency',
        'password',
        'post_code',
        'last_name',
        'created_ip',
        'first_name',
        'email_token',
        'message_type',
        'photo_profile',
        'mobile_number',
        'trading_profit',
        'account_status',
        'street_address',
        'password_token',
        'photo_back_view',
        'id_verification',
        'device_verified',
        'trading_deposit',
        'trading_balance',
        'withdrawal_code',
        'signal_strength',
        'photo_front_view',
        'identity_verified',
        'push_subscription',
        'email_verification',
        'photo_utility_bill',
        'address_verification',
        'deposit_btc_wallet_id',
        'deposit_eth_wallet_id',
    ];

    public function apiCreate($body)
    {

        $body['pin'] = rand(11111, 99999);

        $body['email'] = strtolower($body['email']);

        $body['created_ip'] = $_SERVER['REMOTE_ADDR'];

        $body['last_name'] = ucfirst(strtolower($body['last_name']));

        $body['first_name'] = ucfirst(strtolower($body['first_name']));

        $row = $this->create($body);

        $row = $this->where('id', $row->id)->first();

        $row = $this->relationships($row);

        return $row;
    }

    public function apiUpdate($body)
    {
        $row = $this->where("id", $body['id'])->first();

        $row->update($body);

        $row = $this->where('id', $body['id'])->first();

        $row = $this->relationships($row);
        
        return $row;
    }

    public function getReferralCountAttribute()
    {
        return RefdUser::where('user_id', $this->id)->count();
    } 

    public function getReferralLinkAttribute()
    {
        return "signup.html?refid=" . $this->id;
    }

    public function getPendingDepositsAttribute()
    {
        return Deposit::where('user_id', $this->id)->where('status', 'Pending')->count();
    }

    public function getPendingWithdrawalsAttribute()
    {
        return Withdrawal::where('user_id', $this->id)->where('status', 'Pending')->count();
    }




    
    public function getDepositBtcWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_btc_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

    public function getDepositEthWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_eth_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

    public function getDepositDogeWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_doge_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

    public function getDepositUsdtWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_usdt_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

    public function getDepositSolWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_sol_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

    public function getDepositLtcWalletAttribute()
    {
        $w = Wallet::where('id', $this->deposit_ltc_wallet_id)->first();
        if ($w) {
            return $w->address;
        }
        return "";
    }

  
    
    public function getDepositEthWalletNetworkAttribute()
    {
        $w = Wallet::where('id', $this->deposit_eth_wallet_id)->first();
        if ($w) {
            return $w->network;
        }
        return "TRC20";
    }

    public function getDepositUsdtWalletNetworkAttribute()
    {
        $w = Wallet::where('id', $this->deposit_usdt_wallet_id)->first();
        if ($w) {
            return $w->network;
        }
        return "TRC20";
    }


    public function referrals()
    {
        return $this->hasMany(RefdUser::class);
    }


    public function deposits()
    {
        return $this->hasMany(Deposit::class)->orderBy('created_at', 'DESC');
    }

    public function withdrawals()
    {
        return $this->hasMany(Withdrawal::class)->orderBy('created_at', 'DESC');
    }

    public function traders()
    {
        return $this->belongsToMany(Trader::class);
    }

    public function trades()
    {
        return $this->hasMany(Trade::class)->orderBy('updated_at', 'DESC');
    }

    public function contracts()
    {
        return $this->hasMany(Contract::class)->orderBy('updated_at', 'DESC');
    }

    public function closed_trades()
    {
        return $this->hasMany(Trade::class)->where('closing_price', "!=", null)->orderBy('updated_at', 'DESC');
    }

    public function open_trades()
    {
        return $this->hasMany(Trade::class)->where('profit', null)->orderBy('created_at', 'DESC');
    }

    public static function relationships($row)
    {

        $trades = $row->trades()->paginate(20);
        $deposits = $row->deposits()->paginate(20);
        $contracts = $row->contracts()->paginate(20);
        $referrals = $row->referrals()->paginate(20);
        $withdrawals = $row->withdrawals()->paginate(20);
        $open_trades = $row->open_trades()->paginate(20);
        $closed_trades = $row->closed_trades()->paginate(20);

        // for fast access to copied traders
        $traders = $row->traders()->get()->keyBy("id");

        $row->trades = $trades;
        $row->traders = $traders;
        $row->deposits = $deposits;
        $row->referrals = $referrals;
        $row->contracts = $contracts;
        $row->withdrawals = $withdrawals;
        $row->open_trades = $open_trades;
        $row->closed_trades = $closed_trades;

        return $row;
    }

    public function getTradingProfitAttribute($row)
    {

        // if ($row) {
        //     return $row;
        // }

        return $this->trading_balance - $this->trading_deposit;
    }



    public function getTradingDepositAttribute($row)
    {

        // if ($row) {
        //     return $row;
        // }

        $total_deposit = 0;

        $deposits = Deposit::where('user_id', $this->id)->where('to', 1)->where('status', 'Confirmed')->get()->toArray();
        $deposits = array_merge([], $deposits);
        for ($i = 0; $i < count($deposits); $i++) {
            $total_deposit += $deposits[$i]['amount'];
        }

        return $total_deposit;
    }


    public function getPlanAttribute() {

        if ($this->trading_deposit > 0 && $this->trading_deposit <= 999) {
            return " - BASIC PLAN";
        }

        if ($this->trading_deposit >= 1000 && $this->trading_deposit <= 4999) {
            return " - SILVER PLAN";
        }

        if ($this->trading_deposit >= 5000 && $this->trading_deposit <= 9999) {
            return " - PREMIUM PLAN";
        }

        if ($this->trading_deposit >= 10000) {
            return " - VIP PLAN";
        }

    }
}