<?php

namespace App\Lib;

use App\Constants\Status;
use App\Models\Deposit;
use App\Models\GatewayCurrency;
use App\Models\Transaction;

class Payment
{
    public static function gatewayCurrency()
    {
        return GatewayCurrency::whereHas('method', function ($gate) {
            $gate->where('status', Status::ENABLE);
        })->with('method')->orderby('name')->get();
    }

    public static function handle($gateway, $currency, $amount,$issueData = null, $cardId = null, $fromDetails = false)
    {
        try {
            $gate = GatewayCurrency::whereHas('method', function ($gate) {
                $gate->where('status', Status::ENABLE);
            })
                ->where('method_code', $gateway)
                ->where('currency', $currency)
                ->first();

            if (!$gate) {
                throw new \Exception('Invalid gateway');
            }

            if ($gate->min_amount > $amount || $gate->max_amount < $amount) {
                throw new \Exception('Please follow deposit limit');
            }

            // Calculate charges and final amount
            $charge      = $gate->fixed_charge + ($amount * $gate->percent_charge / 100);
            $payable     = $amount + $charge;
            $finalAmount = $payable * $gate->rate;

            $user                  = auth()->user();
            $data                  = new Deposit();
            $data->user_id         = $user->id;
            $data->card_id          = $cardId;
            $data->is_card_issue   = $issueData ? 1 : 0;
            $data->method_code     = $gate->method_code;
            $data->method_currency = strtoupper($gate->currency);
            $data->amount          = $amount;
            $data->charge          = $charge;
            $data->rate            = $gate->rate;
            $data->final_amount    = $finalAmount;
            $data->btc_amount      = 0;
            $data->btc_wallet      = "";
            $data->trx             = getTrx();
            $data->success_url     = route('user.deposit.history');
            $data->failed_url      = route('user.deposit.history');
            $data->topup_from_details = $fromDetails;
            if($issueData){
                $data->card_issue_details = $issueData;
            }
            $data->save();
            $data->refresh();

            return $data;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public static function chargeUserWallet($amount)
    {
        $user = auth()->user();

        if ($user->balance < $amount) {
            throw new \Exception('Insufficient balance');
        }

        $user->balance -= gs('card_issue_fee');
        $user->save();

        $transaction               = new Transaction();
        $transaction->user_id      = $user->id;
        $transaction->amount       = gs('card_issue_fee');
        $transaction->trx          = getTrx();
        $transaction->trx_type     = '-';
        $transaction->details      = 'Card issue fee';
        $transaction->remark       = 'issue_card_fee';
        $transaction->post_balance = $user->balance;
        $transaction->save();

        $user->balance -=  $amount -  gs('card_issue_fee');
        $user->save();

        $transaction               = new Transaction();
        $transaction->user_id      = $user->id;
        $transaction->amount       = $amount - gs('card_issue_fee');
        $transaction->trx          = getTrx();
        $transaction->trx_type     = '-';
        $transaction->details      = 'Balance deducted for card issue';
        $transaction->remark       = 'issue_card';
        $transaction->post_balance = $user->balance;
        $transaction->save();
    }
}
