<?php

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


/*
|--------------------------------------------------------------------------
| TEST MODE ONLY
|--------------------------------------------------------------------------
*/

if (!paystackIsConfigured()) {

    die(
        "Paystack Test Mode is not configured correctly."
    );

}


/*
|--------------------------------------------------------------------------
| GET UCHENNA'S PAYSTACK CUSTOMER
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        mva.id,
        mva.customer_code,
        mva.validation_status,

        u.fullname

    FROM member_virtual_accounts mva

    INNER JOIN users u
        ON u.id = mva.user_id

    WHERE

        mva.provider = 'paystack'

        AND mva.environment = 'test'

        AND u.fullname = 'Uchenna Okoro'

    ORDER BY mva.id DESC

    LIMIT 1
");


$stmt->execute();


$customer =
    $stmt->fetch(
        PDO::FETCH_ASSOC
    );


if (!$customer) {

    die(
        "Uchenna Okoro's Paystack customer was not found."
    );

}


/*
|--------------------------------------------------------------------------
| FETCH CUSTOMER FROM PAYSTACK
|--------------------------------------------------------------------------
*/

$url =
    $paystackBaseUrl
    .
    '/customer/'
    .
    rawurlencode(
        $customer['customer_code']
    );


$curl =
    curl_init();


curl_setopt_array(
    $curl,
    [

        CURLOPT_URL =>
            $url,

        CURLOPT_RETURNTRANSFER =>
            true,

        CURLOPT_HTTPHEADER => [

            'Authorization: Bearer '
            .
            $paystackSecretKey,

            'Accept: application/json'

        ],

        CURLOPT_TIMEOUT =>
            30

    ]
);


$response =
    curl_exec($curl);


$httpCode =
    curl_getinfo(
        $curl,
        CURLINFO_HTTP_CODE
    );


$curlError =
    curl_error($curl);


curl_close($curl);


$responseData =
    json_decode(
        $response,
        true
    );


$errorMessage = null;

$identified = false;

$paystackCustomer = null;


/*
|--------------------------------------------------------------------------
| CHECK RESPONSE
|--------------------------------------------------------------------------
*/

if ($curlError) {

    $errorMessage =
        "Unable to connect to Paystack: "
        .
        $curlError;

}

elseif (
    $httpCode !== 200
    ||
    empty($responseData['status'])
    ||
    empty($responseData['data'])
) {

    $errorMessage =
        $responseData['message']
        ??
        "Unable to retrieve customer from Paystack.";

}

else {

    $paystackCustomer =
        $responseData['data'];


    $identified =
        !empty(
            $paystackCustomer['identified']
        );


    /*
    |--------------------------------------------------------------------------
    | UPDATE COMPLETE BLISS IF VERIFIED
    |--------------------------------------------------------------------------
    */

    if ($identified) {

        $updateStmt =
            $pdo->prepare("
                UPDATE member_virtual_accounts

                SET validation_status = 'validated'

                WHERE id = ?
            ");


        $updateStmt->execute([
            $customer['id']
        ]);


        $customer['validation_status'] =
            'validated';

    }

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

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

<title>
    Paystack Validation Status
</title>


<style>

body {

    margin: 0;

    padding: 40px 20px;

    font-family: Arial, sans-serif;

    background: #f5f7fb;

    color: #101828;

}


.status-box {

    width: 100%;

    max-width: 650px;

    margin: auto;

    padding: 30px;

    box-sizing: border-box;

    border-radius: 16px;

    background: #ffffff;

}


.badge {

    display: inline-block;

    margin-bottom: 10px;

    padding: 6px 10px;

    border-radius: 999px;

    color: #1d4ed8;

    background: #eff6ff;

    font-size: 11px;

    font-weight: 700;

}


h1 {

    margin: 0 0 20px;

}


.details {

    padding: 18px;

    border-radius: 12px;

    background: #f8fafc;

}


.details span {

    display: block;

    margin-bottom: 5px;

    color: #667085;

    font-size: 11px;

}


.details strong {

    display: block;

    margin-bottom: 18px;

}


.success {

    margin-top: 20px;

    padding: 15px;

    border-radius: 10px;

    color: #15803d;

    background: #ecfdf3;

}


.pending {

    margin-top: 20px;

    padding: 15px;

    border-radius: 10px;

    color: #92400e;

    background: #fffbeb;

}


.error {

    margin-top: 20px;

    padding: 15px;

    border-radius: 10px;

    color: #b42318;

    background: #fef3f2;

}


button {

    width: 100%;

    min-height: 46px;

    margin-top: 20px;

    border: 0;

    border-radius: 9px;

    color: #ffffff;

    background: #2563eb;

    cursor: pointer;

    font-weight: 700;

}

</style>

</head>


<body>


<div class="status-box">


    <span class="badge">
        PAYSTACK TEST MODE
    </span>


    <h1>
        Customer Validation Status
    </h1>


    <div class="details">

        <span>
            Member
        </span>

        <strong>
            <?= htmlspecialchars(
                $customer['fullname']
            ); ?>
        </strong>


        <span>
            Customer Code
        </span>

        <strong>
            <?= htmlspecialchars(
                $customer['customer_code']
            ); ?>
        </strong>


        <span>
            Complete Bliss Status
        </span>

        <strong>
            <?= htmlspecialchars(
                $customer['validation_status']
            ); ?>
        </strong>

    </div>



    <?php if ($errorMessage): ?>


        <div class="error">

            <?= htmlspecialchars(
                $errorMessage
            ); ?>

        </div>


    <?php elseif ($identified): ?>


        <div class="success">

            ✅ Customer identification successful.

            <br><br>

            Paystack has marked this customer as
            <strong>identified</strong>.

        </div>


    <?php else: ?>


        <div class="pending">

            ⏳ Customer is not yet showing as identified.

            <br><br>

            The validation may still be processing.
            Refresh this page to check again.

        </div>


    <?php endif; ?>



    <form method="GET">

        <button type="submit">

            Check Again

        </button>

    </form>


</div>


</body>

</html>