<?php
/**
 * Test file to verify Market Insights banner visibility
 * Access this file to check if your user is admin
 */

session_start();
require_once 'connect.php';

if (!isset($_SESSION['clientId'])) {
    die("❌ Not logged in. Please login first.");
}

$clientId = $_SESSION['clientId'];

// Check user role
$stmt = $pdo->prepare("SELECT clientName, email, userRole, pspId FROM tblclient WHERE clientId = ?");
$stmt->execute([$clientId]);
$user = $stmt->fetch();

?>
<!DOCTYPE html>
<html>
<head>
    <title>Banner Visibility Test</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f7fa;
        }
        .test-box {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .success {
            background: #d1fae5;
            border-left: 4px solid #10b981;
            padding: 15px;
            margin: 15px 0;
            border-radius: 4px;
        }
        .warning {
            background: #fef3c7;
            border-left: 4px solid #f59e0b;
            padding: 15px;
            margin: 15px 0;
            border-radius: 4px;
        }
        .error {
            background: #fee2e2;
            border-left: 4px solid #ef4444;
            padding: 15px;
            margin: 15px 0;
            border-radius: 4px;
        }
        .info {
            background: #dbeafe;
            border-left: 4px solid #3b82f6;
            padding: 15px;
            margin: 15px 0;
            border-radius: 4px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        table td {
            padding: 10px;
            border-bottom: 1px solid #e5e7eb;
        }
        table td:first-child {
            font-weight: bold;
            width: 200px;
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #3b82f6;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            margin: 10px 5px;
        }
        .btn:hover {
            background: #2563eb;
        }
        h1 {
            color: #1e293b;
            margin-bottom: 10px;
        }
        h2 {
            color: #475569;
            font-size: 18px;
            margin-top: 25px;
        }
    </style>
</head>
<body>
    <div class="test-box">
        <h1>🧪 Market Insights Banner Test</h1>
        <p>This page helps you verify if the Market Insights banner will appear on your dashboard.</p>
        
        <h2>Your Account Information</h2>
        <table>
            <tr>
                <td>Client ID:</td>
                <td><?= $clientId ?></td>
            </tr>
            <tr>
                <td>Name:</td>
                <td><?= htmlspecialchars($user['clientName']) ?></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><?= htmlspecialchars($user['email']) ?></td>
            </tr>
            <tr>
                <td>PSP ID:</td>
                <td><?= $user['pspId'] ?></td>
            </tr>
            <tr>
                <td><strong>User Role:</strong></td>
                <td><strong><?= strtoupper($user['userRole']) ?></strong></td>
            </tr>
        </table>
        
        <?php if ($user['userRole'] === 'admin'): ?>
            <div class="success">
                <strong>✅ SUCCESS!</strong><br>
                You are an <strong>ADMIN</strong> user. The Market Insights banner WILL appear on your dashboard.
            </div>
            
            <div class="info">
                <strong>📊 What you should see:</strong><br>
                A purple/blue gradient banner at the top of your dashboard with:
                <ul>
                    <li>Title: "Market Insights & Analytics Dashboard"</li>
                    <li>Description of features</li>
                    <li>Button: "Open Analytics"</li>
                </ul>
            </div>
            
        <?php else: ?>
            <div class="warning">
                <strong>⚠️ NOT AN ADMIN</strong><br>
                You are a <strong><?= strtoupper($user['userRole']) ?></strong> user. The Market Insights banner will NOT appear on your dashboard.
            </div>
            
            <div class="info">
                <strong>💡 To see the banner:</strong><br>
                You need to be upgraded to an admin user. Contact your system administrator or update your role in the database:
                <pre style="background: #f1f5f9; padding: 10px; border-radius: 4px; overflow-x: auto;">UPDATE tblclient SET userRole = 'admin' WHERE clientId = <?= $clientId ?>;</pre>
            </div>
        <?php endif; ?>
        
        <h2>Test Actions</h2>
        <a href="dashboard.php" class="btn">Go to Dashboard</a>
        <?php if ($user['userRole'] === 'admin'): ?>
            <a href="ms.php" class="btn" style="background: #8b5cf6;">Open Market Insights</a>
        <?php endif; ?>
        
        <h2>Troubleshooting</h2>
        <div class="info">
            <strong>If you're admin but don't see the banner:</strong>
            <ol>
                <li><strong>Clear browser cache</strong> (Ctrl+Shift+R or Cmd+Shift+R)</li>
                <li><strong>Hard refresh</strong> the dashboard page</li>
                <li><strong>Check browser console</strong> for JavaScript errors (F12)</li>
                <li><strong>Verify CSS loaded</strong> - view page source and look for .market-insights-banner styles</li>
            </ol>
        </div>
        
        <h2>Database Check</h2>
        <?php
        // Check if userRole column exists
        try {
            $stmt = $pdo->query("SHOW COLUMNS FROM tblclient LIKE 'userRole'");
            $columnExists = $stmt->fetch();
            
            if ($columnExists): ?>
                <div class="success">
                    ✅ Database column 'userRole' exists in tblclient table
                </div>
            <?php else: ?>
                <div class="error">
                    ❌ Database column 'userRole' does NOT exist in tblclient table<br>
                    <strong>Action needed:</strong> Run the database migration script to add the userRole column.
                </div>
            <?php endif;
        } catch (Exception $e) {
            echo '<div class="error">❌ Error checking database: ' . htmlspecialchars($e->getMessage()) . '</div>';
        }
        ?>
        
        <h2>Quick Fix</h2>
        <?php if ($user['userRole'] !== 'admin'): ?>
            <div class="warning">
                <strong>Want to make yourself admin right now?</strong><br>
                Run this SQL query in your database:
                <pre style="background: #f1f5f9; padding: 10px; border-radius: 4px; overflow-x: auto; margin-top: 10px;">UPDATE tblclient 
SET userRole = 'admin' 
WHERE clientId = <?= $clientId ?>;</pre>
                Then refresh this page to verify.
            </div>
        <?php endif; ?>
    </div>
    
    <div class="test-box" style="background: #f8fafc; border: 2px dashed #cbd5e1;">
        <h2>📚 Documentation</h2>
        <p>For more information about the dashboard strategy and why we kept them separate, see:</p>
        <ul>
            <li><strong>DASHBOARD_STRATEGY.md</strong> - Complete explanation of the two-dashboard approach</li>
            <li><strong>client/ms.php</strong> - The Market Insights analytics dashboard (2,764 lines)</li>
            <li><strong>client/dashboard.php</strong> - The operational dashboard (with banner)</li>
        </ul>
    </div>
</body>
</html>
