<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'includes/ProxyHelper.php';
$proxy = new ProxyHelper();

$sessionProfile = $_SESSION['cookie_profile'] ?? 'Not Set';
$activeProfileFile = __DIR__ . '/cookies/active_profile.txt';
$fileProfile = file_exists($activeProfileFile) ? trim(file_get_contents($activeProfileFile)) : 'Empty/Not Found';

// Get current cookies loaded
$cookieFile = __DIR__ . "/cookies/cookie_data_" . ($sessionProfile !== 'Not Set' ? preg_replace('/[^a-zA-Z0-9_-]/', '', $sessionProfile) : ($fileProfile !== 'Empty/Not Found' ? preg_replace('/[^a-zA-Z0-9_-]/', '', $fileProfile) : 'default')) . ".json";

$cookieString = 'No Cookie File found';
$lastStatus = 'N/A';
$updatedAt = 'N/A';
if (file_exists($cookieFile)) {
    $cData = json_decode(file_get_contents($cookieFile), true);
    $cookieString = $cData['cookie_string'] ?? 'No Cookie String';
    $lastStatus = $cData['last_status'] ?? 'N/A';
    $updatedAt = isset($cData['updated_at']) ? date('Y-m-d H:i:s', $cData['updated_at']) : 'N/A';
}
// Perform diagnostic fetch
$headers = ['client: bris', 'X-Requested-With: XMLHttpRequest'];
$resp = $proxy->fetch('https://bdris.gov.bd/br/application', 'GET', [], false, 'https://bdris.gov.bd/', $headers);
$isHtml = (stripos(($resp['body'] ?? ''), '<title>Login</title>') !== false || stripos(($resp['body'] ?? ''), 'login-page') !== false);
$status = $resp['status'] ?? 0;

?>
<!DOCTYPE html>
<html>
<head>
    <title>BDRIS Session Diagnostics</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light p-5">
<div class="container bg-white p-4 shadow rounded">
    <h2 class="mb-4 text-primary">BDRIS Session Diagnostics</h2>
    
    <table class="table table-bordered">
        <tr>
            <th>$_SESSION['cookie_profile']</th>
            <td><strong class="text-danger"><?php echo htmlspecialchars($sessionProfile); ?></strong></td>
        </tr>
        <tr>
            <th>active_profile.txt</th>
            <td><strong><?php echo htmlspecialchars($fileProfile); ?></strong></td>
        </tr>
        <tr>
            <th>Cookie File Path</th>
            <td><code><?php echo htmlspecialchars($cookieFile); ?></code></td>
        </tr>
        <tr>
            <th>Cookie Status in File</th>
            <td><span class="badge bg-info"><?php echo htmlspecialchars($lastStatus); ?></span></td>
        </tr>
        <tr>
            <th>Cookie Last Updated</th>
            <td><?php echo htmlspecialchars($updatedAt); ?></td>
        </tr>
        <tr>
            <th>Cookie Preview</th>
            <td><pre style="word-break: break-all; white-space: pre-wrap;"><?php echo htmlspecialchars(substr($cookieString, 0, 100)) . '...'; ?></pre></td>
        </tr>
    </table>

    <h4 class="mt-4">BDRIS Connection Test Result</h4>
    <div class="alert <?php echo ($status === 200 && !$isHtml) ? 'alert-success' : 'alert-danger'; ?>">
        <strong>HTTP Status:</strong> <?php echo $status; ?><br>
        <strong>Session Status:</strong> <?php echo $isHtml ? 'Expired / Blocked' : 'Valid (Active)'; ?><br>
        <strong>Body Preview:</strong>
        <pre class="mt-2" style="background: rgba(0,0,0,0.05); padding: 10px;"><?php echo htmlspecialchars(substr($resp['body'] ?? '', 0, 500)); ?></pre>
    </div>
</div>
</body>
</html>
