<?php

namespace Server\Database\Models\Traits;

use Server\Database\Models\Contract;
use Server\Database\Models\Withdrawal;

trait MiningTrait {

    public function calculateMiningBalance($currency)
    {
        $total_mined = 0;
        $total_withdrawals = 0; 

        $mining_contracts =  Contract::where('user_id', $this->id)->where('currency', $currency)->get()->toArray();
        $mining_contracts = array_merge([], $mining_contracts);
        for ($i = 0; $i < count($mining_contracts); $i++) {
            $total_mined += $mining_contracts[$i]['mined'];
        }

        $mining_withdrawals = Withdrawal::where('user_id', $this->id)->where('from', $currency)->get()->toArray();
        $mining_withdrawals = array_merge([], $mining_withdrawals);
        for ($i = 0; $i < count($mining_withdrawals); $i++) {
            if ($mining_withdrawals[$i]['status'] != "Failed") {
                $total_withdrawals += $mining_withdrawals[$i]['amount'];
            }
        }

        $total_mined = $total_mined - $total_withdrawals;

        // return $total_mined;
        return round($total_mined, 6);
    }

    public function calculateMiningHashrate($currency)
    {
        $hashrate = 0;

        $mining_contracts =  Contract::where('user_id', $this->id)->where('currency', $currency)->get()->toArray();

        $mining_contracts = array_merge([], $mining_contracts);
        for ($i = 0; $i < count($mining_contracts); $i++) {
            $hashrate += $mining_contracts[$i]['hashrate'];
        }

        return $hashrate;
    }

    public function calculateMiningSpeedPerSecond($currency)
    {
        $speed_per_second = 0;

        $mining_contracts =  Contract::where('user_id', $this->id)->where('currency', $currency)->get()->toArray();

        $mining_contracts = array_merge([], $mining_contracts);
        for ($i = 0; $i < count($mining_contracts); $i++) {
            $speed_per_second += $mining_contracts[$i]['speed_per_second'];
        }

        return $speed_per_second;
    }


    // mining balance

    public function getMiningBalanceBnbAttribute($row)
    {

        if ($row > 0) {
            return $row;
        } 

        return $this->calculateMiningBalance(5);
    }
    
    public function getMiningBalanceBitcoinAttribute($row)
    {

        if ($row > 0) {
            return $row;
        } 

        return $this->calculateMiningBalance(2);
    }

    public function getMiningBalanceEthereumAttribute($row)
    {
        
        if ($row > 0) {
            return $row;
        } 

        return $this->calculateMiningBalance(3);
    }

    public function getMiningBalanceDogecoinAttribute($row)
    {


        if ($row > 0) {
            return $row;
        } 

        return $this->calculateMiningBalance(4);
    }


    // mining hashrate

    public function getMiningHashrateBnbAttribute()
    {
        return $this->calculateMiningHashrate(5);
    }

    public function getMiningHashrateBitcoinAttribute()
    {
        return $this->calculateMiningHashrate(2);
    }

    public function getMiningHashrateEthereumAttribute()
    {
        return $this->calculateMiningHashrate(3);
    }

    public function getMiningHashrateDogecoinAttribute()
    {
        return $this->calculateMiningHashrate(4);
    }


    // mining speed

    public function getMiningSpeedPsBitcoinAttribute()
    {
        return $this->calculateMiningSpeedPerSecond(2);
    }

    public function getMiningSpeedPsEthereumAttribute()
    {
        return $this->calculateMiningSpeedPerSecond(3);
    }

    public function getMiningSpeedPsDogecoinAttribute()
    {
        return $this->calculateMiningSpeedPerSecond(4);
    }

    public function getMiningSpeedPsBnbAttribute()
    {
        return $this->calculateMiningSpeedPerSecond(5);
    }



}