<?php

namespace App\Http\Controllers\InvoicePayment;

use App\Http\Controllers\Controller;
use App\Http\Requests\Invoice\CheckInvoiceRequest;
use App\Services\WhmcsServices;
use Illuminate\Http\Request;

class InvoicePaymentController extends Controller
{

    protected WhmcsServices $whmcsServices;
    public function __construct(WhmcsServices $whmcsServices)
    {
        $this->whmcsServices = $whmcsServices;
    }

    public function index(Request $request){
        return view('InvoicePayment.index');
    }


    public function checkInvoice(CheckInvoiceRequest $request){

        $order = $this->whmcsServices->getInvoiceDetailByInvoiceNumber($request->invoiceNumber);
        $client = $this->whmcsServices->getClientById($order['userid']);
        $payment = [];
        $payment['usd'] = $this->convertCurrency($client['currency_code'], 'USD', $order['total']);
        $payment['afn'] = $this->convertCurrency($client['currency_code'], 'AFN', $order['total']);
        return view('InvoicePayment.result', compact('order', 'payment', 'client'));
    }

    protected function convertCurrency($from, $to, $amount) {
        if ($from === $to) return $amount;
    
        $usdRate = 70.80;
        $rates = ['USD_AFN' => $usdRate, 'AFN_USD' => round(1 / $usdRate, 6)];
        
        return round($amount * ($rates["{$from}_{$to}"] ?? 0), 2);
    }
    
    




}
