<?php

include "../includes/member_auth.php";
include "../config/database.php";

$user_id = $_SESSION['user_id'];

/*
|--------------------------------------------------------------------------
| Get Member Information
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT
    fullname,
    email,
    wallet_balance
FROM users
    WHERE id=?
    LIMIT 1
");

$stmt->execute([$user_id]);

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

if(!$user){

    header("Location: dashboard.php");
    exit();

}
/*
|--------------------------------------------------------------------------
| Get Live Virtual Account
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        bank_name,
        account_name,
        account_number,
        active,
        assigned
    FROM member_virtual_accounts
    WHERE
        user_id = ?
        AND provider = 'paystack'
        AND environment = 'live'
        AND active = 1
        AND assigned = 1
    LIMIT 1
");

$stmt->execute([$user_id]);

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


/*
|--------------------------------------------------------------------------
| Get Funding Account Request
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        status,
        requested_at,
        failure_reason
    FROM funding_account_requests
    WHERE
        user_id = ?
        AND provider = 'paystack'
    LIMIT 1
");

$stmt->execute([$user_id]);

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

/*
|--------------------------------------------------------------------------
| Wallet Transactions
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        transaction_type,
        amount,
        balance_before,
        balance_after,
        reference,
        description,
        status,
        created_at
    FROM wallet_transactions
    WHERE user_id=?
    ORDER BY id DESC
");

$stmt->execute([$user_id]);

$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

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

<title>My Wallet</title>

<link rel="stylesheet" href="../assets/css/admin.css">

<link rel="stylesheet" href="../assets/css/responsive.css">

<link rel="stylesheet" href="../assets/css/member.css">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

</head>

<body>


<?php include "../includes/member_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/member_topbar.php"; ?>


<div class="main-content">


<!-- PAGE HEADER -->

<div class="page-header">

<div>

<h1>
<i class="fas fa-wallet"></i>
My Wallet
</h1>

<p>
View your wallet balance and transaction history.
</p>

</div>

</div>


<!-- WALLET SUMMARY -->

<div class="dashboard-grid">


<div class="dashboard-card">

<div class="card-icon wallet">

<i class="fas fa-wallet"></i>

</div>

<h4>Wallet Balance</h4>

<h2>
₦<?= number_format($user['wallet_balance'],2); ?>
</h2>

</div>


<div class="dashboard-card wallet-funding-card">

    <div class="card-icon bank">

        <i class="fas fa-building-columns"></i>

    </div>


    <h4>
        Personal Funding Account
    </h4>


    <?php if ($virtualAccount): ?>


        <div class="wallet-bank-details">

            <span>

                <?= htmlspecialchars(
                    $virtualAccount['bank_name']
                ); ?>

            </span>


            <strong>

                <?= htmlspecialchars(
                    $virtualAccount['account_number']
                ); ?>

            </strong>


            <small>

                <?= htmlspecialchars(
                    $virtualAccount['account_name']
                ); ?>

            </small>

        </div>


    <?php elseif (
        $fundingRequest &&
        in_array(
            $fundingRequest['status'],
            ['pending', 'processing'],
            true
        )
    ): ?>


        <div class="wallet-funding-status pending">

            <i class="fas fa-clock"></i>

            <span>

                <strong>
                    Activation Pending
                </strong>

                <small>
                    Your personal funding account request
                    is being processed.
                </small>

            </span>

        </div>


    <?php elseif (
        $fundingRequest &&
        $fundingRequest['status'] === 'failed'
    ): ?>


        <div class="wallet-funding-status failed">

            <i class="fas fa-circle-exclamation"></i>

            <span>

                <strong>
                    Activation Incomplete
                </strong>

                <small>
                    Please try again or contact Complete Bliss support.
                </small>

            </span>

        </div>


        <a
            href="activate-funding-account.php"
            class="wallet-activate-btn"
        >

            <i class="fas fa-rotate-right"></i>

            Try Again

        </a>


    <?php else: ?>


        <p class="wallet-no-account">

            Activate your personal funding account
            to fund your Complete Bliss wallet
            through bank transfer.

        </p>


        <a
            href="activate-funding-account.php"
            class="wallet-activate-btn"
        >

            <i class="fas fa-building-columns"></i>

            Activate Funding Account

        </a>


    <?php endif; ?>


</div>

</div>



<!-- WALLET INFORMATION -->

<div class="activity-card">


<div class="activity-title-wrap">

<i class="fas fa-circle-info"></i>

<h3>Wallet Information</h3>

</div>


<div class="wallet-info">

<p>

<i class="fas fa-shield-halved"></i>

Your wallet is used to automatically fund your scheme contributions.

</p>

<p>

<i class="fas fa-money-bill-transfer"></i>

Money added to your wallet can be used for scheduled contributions.

</p>

</div>


</div>



<!-- TRANSACTION HISTORY -->

<div class="activity-card">


<div class="activity-title-wrap">

<i class="fas fa-clock-rotate-left"></i>

<h3>Wallet Transactions</h3>

</div>


<div class="activity-list">


<?php if(count($transactions) > 0): ?>


<?php foreach($transactions as $transaction): ?>


<div class="activity-item">


<?php if($transaction['transaction_type']=="deposit"): ?>

<div class="activity-icon success">

<i class="fas fa-arrow-down"></i>

</div>

<?php else: ?>

<div class="activity-icon pending">

<i class="fas fa-arrow-up"></i>

</div>

<?php endif; ?>


<div class="activity-details">

<h4>

<?= ucfirst(
    htmlspecialchars($transaction['transaction_type'])
); ?>

</h4>


<p>

<?= htmlspecialchars(
    $transaction['description'] ?? 'Wallet transaction'
); ?>

<br>

<small>

<?= date(
    "d M Y, h:i A",
    strtotime($transaction['created_at'])
); ?>

</small>

</p>

</div>


<span class="<?=

$transaction['transaction_type']=="deposit"

? "amount-positive"

: "amount-negative"

?>">

<?= $transaction['transaction_type']=="deposit"
    ? "+"
    : "-"
?>

₦<?= number_format($transaction['amount'],2); ?>

</span>


</div>


<?php endforeach; ?>


<?php else: ?>


<div class="activity-item">


<div class="activity-icon pending">

<i class="fas fa-wallet"></i>

</div>


<div class="activity-details">

<h4>No Transactions Yet</h4>

<p>

Your wallet transactions will appear here.

</p>

</div>


<span>--</span>


</div>


<?php endif; ?>


</div>


</div>


</div>


<script src="../assets/js/admin.js"></script>


</body>

</html>