<?php

include "../includes/admin_auth.php";
include "../config/paystack.php";


/*
|--------------------------------------------------------------------------
| Check Configuration
|--------------------------------------------------------------------------
*/

if (!paystackIsConfigured()) {

    die(
        "Paystack test configuration is not valid."
    );

}


/*
|--------------------------------------------------------------------------
| Test API Request
|--------------------------------------------------------------------------
|
| We simply ask Paystack for the list of Nigerian banks.
| This does NOT create a payment or move money.
|
*/

$url =
    $paystackBaseUrl
    .
    "/bank?country=nigeria&currency=NGN";


$curl = curl_init();


curl_setopt_array(
    $curl,
    [

        CURLOPT_URL =>
            $url,

        CURLOPT_RETURNTRANSFER =>
            true,

        CURLOPT_HTTPHEADER =>
            [

                "Authorization: Bearer "
                .
                $paystackSecretKey,

                "Accept: application/json"

            ],

        CURLOPT_TIMEOUT =>
            20

    ]
);


$response =
    curl_exec($curl);


$httpCode =
    curl_getinfo(
        $curl,
        CURLINFO_HTTP_CODE
    );


$curlError =
    curl_error($curl);


curl_close($curl);


$data =
    json_decode(
        $response,
        true
    );

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta
    name="viewport"
    content="width=device-width, initial-scale=1.0"
>

<title>
    Paystack Connection Test
</title>

<style>

body {
    font-family: Arial, sans-serif;
    background: #f5f7fb;
    padding: 40px;
}

.test-box {
    max-width: 650px;
    margin: auto;
    padding: 30px;
    border-radius: 15px;
    background: #ffffff;
}

.success {
    color: #15803d;
}

.error {
    color: #b42318;
}

</style>

</head>

<body>


<div class="test-box">


<?php if ($curlError): ?>


    <h2 class="error">
        Connection Failed
    </h2>

    <p>
        <?= htmlspecialchars(
            $curlError
        ); ?>
    </p>


<?php elseif (
    $httpCode === 200
    &&
    !empty($data['status'])
): ?>


    <h2 class="success">
        ✅ Paystack Connection Successful
    </h2>


    <p>
        Complete Bliss successfully connected
        to Paystack Test Mode.
    </p>


    <p>
        Paystack response:
        <strong>
            <?= htmlspecialchars(
                $data['message']
                ?? 'Successful'
            ); ?>
        </strong>
    </p>


    <p>
        Banks returned:
        <strong>
            <?= count(
                $data['data']
                ?? []
            ); ?>
        </strong>
    </p>


<?php else: ?>


    <h2 class="error">
        Paystack Connection Failed
    </h2>


    <p>
        HTTP Status:
        <?= (int)$httpCode; ?>
    </p>


    <p>
        <?= htmlspecialchars(
            $data['message']
            ??
            'Paystack did not accept the request.'
        ); ?>
    </p>


<?php endif; ?>


</div>


</body>

</html>