<?php 

namespace App\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BamboraService{

    public $BAMBORA_MERCHANT_ID='383611826',$BAMBORA_API_PROFILE='BF80Dd278B474b3E9346B991A1eFe849',$BAMBORA_API_PAYMENT='0c6Fc4595e31420ca2db0a5975785264',$BAMBORA_TEST_MODE=true;
    
    public function create_profile(Request $request)
    {
        $url = 'https://api.na.bambora.com/v1/profiles';

        $data = array(
            'card' => array(
                'name' => $request->name,
                'number' => $request->card_number,
                'expiry_month' => $request->expiry_month,
                'expiry_year' => $request->expiry_year,
                'cvd' => $request->cvc,
            ),
            'billing' => array(
                'name' => $request->name,
                'email_address' => Auth::user()->email,
                'phone_number' => $request->phone_number,
                'address_line1' => $request->address_line,
                'province' => $request->state,
                'city' => $request->city,
                'country' => $request->country,
                'postal_code' => $request->zipcode
            ),
        );

        $payloadJson = json_encode($data);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJson);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Authorization: Passcode ' . base64_encode($this->BAMBORA_MERCHANT_ID.':'.$this->BAMBORA_API_PROFILE),
        ]);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);

        // Decode the response
        return json_decode($result, true);
    }

    public function get_card_from_profile($profile_id)
    {
        $url = 'https://api.na.bambora.com/v1/profiles/'.$profile_id.'/cards';

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Authorization: Passcode ' . base64_encode($this->BAMBORA_MERCHANT_ID.':'.$this->BAMBORA_API_PROFILE),
        ]);

        $result = curl_exec($ch);

        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);

        // Decode the response
        return json_decode($result, true);
    }

    public function make_payment($user,$card_id,$amount){
        $url = 'https://api.na.bambora.com/v1/payments';
            $payload = [
                'amount' => $amount,
                'payment_method' => 'payment_profile',
                'payment_profile' => [
                    'complete' => true,
                    'customer_code' => $user->profile_id,
                    'card' => $card_id,
                ],
                // Add any other required parameters as needed
            ];

            // Convert the payload to JSON
            $payloadJson = json_encode($payload);

            // Set the cURL options
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payloadJson);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json',
                'Authorization: Passcode ' . base64_encode($this->BAMBORA_MERCHANT_ID.':'.$this->BAMBORA_API_PAYMENT),
            ]);

            $response = curl_exec($ch);

            // Check for cURL errors
            if (curl_errno($ch)) {
                return curl_error($ch);
            }
            curl_close($ch);

            return json_decode($response, true);
    }
}