<?php

namespace Server\Database\Models;

use Server\Library\Services\Sender;
use Server\Library\Services\Renderer;
use Server\Database\Models\Traits\UpdatedTrait;

class Deposit extends ApiModel
{
    use UpdatedTrait;

    protected $fillable = [
        'to',
        'link',
        'code',
        'proof',
        'amount',
        'status',
        'comment',
        'user_id',
        'currency',
        'timestamp',
        'updated_at_day',
        'wallet_address',
        'investment_term',
        'crypto_currency',
        'updated_at_month',
        'amount_in_crypto',
        'created_by_admin',
    ];

    public function getCommentAttribute($row) {

        if ($row) {
            return $row;
        }

        if ($this->to == 1) {
            return 'Trading';
        }

        if ($this->to == 2) {
            return 'Bitcoin Mining';
        }

        if ($this->to == 3) {
            return 'Ethereum Mining';
        }
        
        if ($this->to == 4) {
            return 'Dogecoin Mining';
        }

        if ($this->to == 5) {
            return 'Binance Coin Mining';
        }
    }

    public function apiCreate($body)
    {

        $row = $this->create($body);
        $user = User::where("id", $row->user_id)->first(); 
        $user = User::relationships($user);

        return $user;
    }

    public function apiUpdate($body)
    {

        $row = $this->where("id", $body['id'])->first();
        
        $user = User::where("id", $row->user_id)->first();

        // check for change
        if ($body['status'] != $row->status && $row->to == 1) {

            // from pending to confirmed
            if ($row->status == 'Pending' && $body['status'] == 'Confirmed') {
                 $user->update(['trading_balance' => $user->trading_balance + $row->amount]);
            } 

            // from pending to failed

            // from confirmed to pending
            if ($row->status == 'Confirmed' && $body['status'] == 'Pending') {
                 $user->update(['trading_balance' => $user->trading_balance - $row->amount]);
            } 

            // from confirmed to failed
            if ($row->status == 'Confirmed' && $body['status'] == 'Failed') {
                 $user->update(['trading_balance' => $user->trading_balance - $row->amount]);
            } 

            // from failed to pending

            // from failed to confirmed
            if ($row->status == 'Failed' && $body['status'] == 'Confirmed') {
                 $user->update(['trading_balance' => $user->trading_balance + $row->amount]);
            } 
        }


        // send payment recieved confirmation
        if ($row->status == 'Pending' && $body['status'] == 'Confirmed') {
            $sender = new Sender();   
            $renderer = new Renderer();    

            $subject = "Deposit Confirmed";
            $data = "Your deposit of " . $row->amount . " " . $user->currency ." has been confirmed"; 

            $data = $renderer->render('email2.html', ['title' => $subject, 'body' => $data]);

            $sender->sendEmail([$user->email], $data, $subject);        
        } 
        
        $row->update($body);

        $user = User::where("id", $row->user_id)->first();
        $user = User::relationships($user);

        return $user;
    }

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

        $user = User::where('id', $row->user_id)->first();

        if ($row->status == 'Confirmed') {
            $user->update(['trading_balance' => $user->trading_balance - $row->price]);
        }

        $row->delete();

        $user = User::where("id", $user_id)->first();
        $user = User::relationships($user);

        return $user;
    }
}