<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$config_path = dirname(__DIR__, 2) . '/include/config.php';
if (file_exists($config_path)) {
    include($config_path);
} else {
    echo '<div class="alert alert-danger">Configuration file not found.</div>';
    exit();
}

// Verify admin
$stmt = $pdo->prepare("SELECT is_admin FROM users WHERE id = :user_id");
$stmt->bindParam(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$stmt->execute();
$admin = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$admin || $admin['is_admin'] != 1) {
    echo '<div class="alert alert-danger">Unauthorized access.</div>';
    exit();
}

$number_id = filter_var($_POST['number_id'] ?? 0, FILTER_VALIDATE_INT);

if (!$number_id) {
    echo '<div class="alert alert-danger">Invalid number ID.</div>';
    exit();
}

try {
    $stmt = $pdo->prepare("
        SELECT an.*, u.username, u.full_name, u.email, u.phone
        FROM active_number an
        LEFT JOIN users u ON an.user_id = u.id
        WHERE an.id = :id
        LIMIT 1
    ");
    $stmt->bindParam(':id', $number_id, PDO::PARAM_INT);
    $stmt->execute();
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo '<div class="alert alert-danger">Database error: ' . htmlspecialchars($e->getMessage()) . '</div>';
    exit();
}

if (!$row) {
    echo '<div class="alert alert-warning">Number record not found.</div>';
    exit();
}

// Status labels
if ($row['status'] == '1')     { $status_text = 'Completed'; $status_badge = 'success'; }
elseif ($row['status'] == '2') { $status_text = 'Pending';   $status_badge = 'warning'; }
elseif ($row['status'] == '3') { $status_text = 'Cancelled'; $status_badge = 'danger'; }
else                           { $status_text = 'Unknown';   $status_badge = 'secondary'; }

$active_text  = $row['active_status'] == '2' ? 'Active'   : 'Inactive';
$active_badge = $row['active_status'] == '2' ? 'primary'  : 'secondary';
$display_name = !empty($row['full_name']) ? $row['full_name'] : ($row['username'] ?? '—');
?>
<div class="row">
    <div class="col-md-6">
        <h6 class="font-weight-bold text-primary mb-3"><i class="fas fa-mobile-alt mr-2"></i>Number Info</h6>
        <table class="table table-sm table-bordered">
            <tr>
                <th class="bg-light" width="40%">Number</th>
                <td><span style="font-family:monospace;color:#4e73df;font-weight:600;"><?php
                $num = $row['number'];
                // Avoid double + — smspva numbers already include the + prefix
                echo htmlspecialchars(strpos($num, '+') === 0 ? $num : '+' . $num);
            ?></span></td>
            </tr>
            <tr>
                <th class="bg-light">Number ID</th>
                <td><code><?php echo htmlspecialchars($row['number_id']); ?></code></td>
            </tr>
            <tr>
                <th class="bg-light">Order ID</th>
                <td><code style="font-size:0.8rem;"><?php echo htmlspecialchars($row['order_id']); ?></code></td>
            </tr>
            <tr>
                <th class="bg-light">Service</th>
                <td><strong><?php echo htmlspecialchars($row['service_name']); ?></strong> <small class="text-muted">(<?php echo htmlspecialchars($row['service_id']); ?>)</small></td>
            </tr>
            <tr>
                <th class="bg-light">Server ID</th>
                <td><?php echo htmlspecialchars($row['server_id']); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Provider</th>
                <td>
                    <?php
                    $prov = $row['provider'] ?? 'smscodes';
                    if ($prov === 'smspva'):
                    ?><span class="badge badge-primary"><i class="fas fa-server mr-1"></i>Server1 — smspva.com</span><?php
                    else:
                    ?><span class="badge badge-success"><i class="fas fa-satellite-dish mr-1"></i>Server2 — smscodes.io</span><?php
                    endif; ?>
                </td>
            </tr>
            <?php if (!empty($row['operator'])): ?>
            <tr>
                <th class="bg-light">Operator</th>
                <td><span class="badge badge-secondary"><?php echo htmlspecialchars($row['operator']); ?></span></td>
            </tr>
            <?php endif; ?>
            <tr>
                <th class="bg-light">Price</th>
                <td><strong style="color:#4e73df;">$<?php echo number_format($row['service_price'], 2); ?></strong></td>
            </tr>
            <tr>
                <th class="bg-light">Purchase Date</th>
                <td><?php echo date('M d, Y h:i A', strtotime($row['buy_time'])); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Status</th>
                <td><span class="badge badge-<?php echo $status_badge; ?>"><?php echo $status_text; ?></span></td>
            </tr>
            <tr>
                <th class="bg-light">Active Status</th>
                <td><span class="badge badge-<?php echo $active_badge; ?>"><?php echo $active_text; ?></span></td>
            </tr>
        </table>
    </div>
    <div class="col-md-6">
        <h6 class="font-weight-bold text-primary mb-3"><i class="fas fa-user mr-2"></i>User Info</h6>
        <table class="table table-sm table-bordered">
            <tr>
                <th class="bg-light" width="40%">User ID</th>
                <td><?php echo htmlspecialchars($row['user_id']); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Username</th>
                <td><?php echo htmlspecialchars($row['username'] ?? '—'); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Full Name</th>
                <td><?php echo htmlspecialchars($display_name); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Email</th>
                <td><?php echo htmlspecialchars($row['email'] ?? '—'); ?></td>
            </tr>
            <tr>
                <th class="bg-light">Phone</th>
                <td><?php echo htmlspecialchars($row['phone'] ?? '—'); ?></td>
            </tr>
        </table>

        <?php if (!empty($row['sms_text'])): ?>
        <h6 class="font-weight-bold text-success mt-3 mb-2"><i class="fas fa-sms mr-2"></i>SMS Received</h6>
        <div style="background:#f8f9fc;padding:0.75rem;border-radius:0.35rem;font-family:'Courier New',monospace;font-size:0.9rem;word-break:break-all;border:1px solid #e3e6f0;">
            <?php echo htmlspecialchars($row['sms_text']); ?>
        </div>
        <?php else: ?>
        <div class="alert alert-secondary mt-3 py-2">
            <i class="fas fa-info-circle mr-1"></i> No SMS received for this number yet.
        </div>
        <?php endif; ?>
    </div>
</div>