<?php

require_once 'includes/provider_auth.php';
require_once '../config/constants.php';

$provider_id = $_SESSION['provider']['id'];

/* GET PROFILE */

$profile_api = API_URL . "providers/full-profile.php?provider_id=" . $provider_id;

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $profile_api,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-KEY: ' . API_KEY,
        'Authorization: Bearer ' . $_SESSION['provider_token']
    ]
]);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

$subscription = $result['data']['subscription'] ?? null;
$today_leads = $result['data']['today_leads'] ?? 0;


/* GET PLANS */

$plans_api = API_URL . "plans/list.php";

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $plans_api,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-KEY: ' . API_KEY
    ]
]);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

$plans = $result['data'] ?? [];

?>

<?php include '../admin/partials/layouts/layoutTop.php' ?>

<div class="dashboard-main-body">


    <!-- PAGE HEADER -->

    <div class="mb-24">

        <h5 class="fw-semibold">Subscription & Billing</h5>

        <p class="text-secondary-light">

            Manage your plan and lead limits

        </p>

    </div>


    <?php if ($subscription): ?>

        <!-- ACTIVE PLAN BANNER -->

        <div class="card mb-24 border-success">

            <div class="card-body p-24">

                <div class="d-flex justify-content-between align-items-center">

                    <div>

                        <h5 class="text-success mb-4">

                            Active Plan:
                            <?php echo htmlspecialchars($subscription['plan_name']); ?>

                        </h5>

                        <p class="mb-0 text-secondary-light">

                            Valid until

                            <strong>

                                <?php echo date("d M Y", strtotime($subscription['expiry_date'])); ?>

                            </strong>

                        </p>

                    </div>

                    <div>

                        <h3 class="fw-bold">

                            ₹
                            <?php echo number_format($subscription['plan_price']); ?>

                        </h3>

                        <span class="text-secondary-light">
                            per
                            <?php echo $subscription['validity_days']; ?> days
                        </span>

                    </div>

                </div>

            </div>

        </div>


        <!-- LEAD USAGE -->

        <?php

        $remaining = max(0, $subscription['daily_lead_limit'] - $today_leads);

        $percent = ($subscription['daily_lead_limit'] > 0)
            ? ($today_leads / $subscription['daily_lead_limit']) * 100
            : 0;

        ?>

        <div class="card mb-24">

            <div class="card-body p-24">

                <h6 class="mb-16">Daily Lead Usage</h6>

                <div class="mb-8">

                    <strong>
                        <?php echo $today_leads ?>
                    </strong>
                    of
                    <?php echo $subscription['daily_lead_limit'] ?> leads used

                </div>

                <div class="progress mb-8" style="height:8px">

                    <div class="progress-bar bg-primary" style="width:<?php echo $percent ?>%">

                    </div>

                </div>

                <small class="text-secondary-light">

                    <?php echo $remaining ?> leads remaining today

                </small>

            </div>

        </div>

    <?php endif; ?>


    <!-- PRICING PLANS -->

    <div class="row gy-4">

        <?php foreach ($plans as $plan): ?>

            <?php

            $isCurrent = ($subscription && $subscription['plan_id'] == $plan['id']);

            $isRecommended = ($plan['price'] > 1000); // example logic
        
            ?>

            <div class="col-lg-4">

                <div class="card h-100 text-center border radius-12
<?php echo $isCurrent ? 'border-success shadow' : ''; ?>">

                    <div class="card-body p-32">


                        <?php if ($isRecommended): ?>

                            <span class="badge bg-primary mb-12">

                                Recommended

                            </span>

                        <?php endif; ?>


                        <h5 class="fw-semibold mb-8">

                            <?php echo htmlspecialchars($plan['name']); ?>

                        </h5>

                        <h2 class="fw-bold mb-16">

                            ₹
                            <?php echo number_format($plan['price']); ?>

                        </h2>

                        <span class="text-secondary-light d-block mb-16">

                            <?php echo $plan['validity_days']; ?> Days Validity

                        </span>


                        <ul class="list-unstyled mb-24">

                            <li class="mb-8">
                                ✔
                                <?php echo $plan['categories_allowed']; ?> Categories
                            </li>

                            <li class="mb-8">
                                ✔
                                <?php echo $plan['daily_lead_limit']; ?> Leads / Day
                            </li>

                            <li class="mb-8">
                                ✔ Lead Notifications
                            </li>

                        </ul>


                        <p class="text-secondary-light small mb-24">

                            <?php echo htmlspecialchars($plan['description']); ?>

                        </p>


                        <?php if ($isCurrent): ?>

                            <button class="btn btn-success w-100" disabled>

                                Current Plan

                            </button>

                        <?php else: ?>

                            <a href="checkout.php?plan_id=<?php echo $plan['id']; ?>" class="btn btn-primary w-100">

                                <?php

                                if (!$subscription) {
                                    echo "Activate Plan";
                                } else if ($plan['price'] > $subscription['plan_price']) {
                                    echo "Upgrade Plan";
                                } else {
                                    echo "Downgrade Plan";
                                }

                                ?>

                            </a>

                        <?php endif; ?>


                    </div>

                </div>

            </div>

        <?php endforeach; ?>

    </div>


</div>

<?php include '../admin/partials/layouts/layoutBottom.php' ?>