<?php

namespace Server\Database\Models;

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

class Withdrawal extends ApiModel
{

    use UpdatedTrait;

    protected $fillable = [
        'ssn',
        'from',
        'amount',
        'status',
        'user_id',
        'currency',
        'bank_name',
        'account_name',
        'paypal_email',
        'payment_method',
        'account_number',
        'routing_number',
        'wallet_address',
        'updated_at_month',
        'updated_at_day'
    ];

    public function apiCreate($body)
    {
        // determine currency

        $this->create($body);

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

        if ($body['from'] == 1) {
            $user->update(['trading_balance' => $user->trading_balance - $body['amount']]); 
        }

        $sender = new Sender();   
        $renderer = new Renderer();    

        $subject = "Pending Withdrawal";
        $data = "Your withdrawal request has been received (".$body['amount']." ".$body['currency']."), it will be processed within 5 working days."; 

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

        $sender->sendEmail([$user->email], $data, $subject);  

        $user = User::where("id", $body["user_id"])->first();
        return User::relationships($user);
    }

    public function apiUpdate($body)
    {

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

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

        // detect change
        if ($body['status'] != $row->status) {

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

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

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

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

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

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

            // if ($body['status'] == 'Completed') {
            //     $user = $user->update(['trading_balance' => $user->trading_balance - $row->amount]);
            // } else {
            //     $user = $user->update(['trading_balance' => $user->trading_balance + $row->amount]);
            // }
        }

        $row->update($body);

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

        return $user;
    }
}