<?php
/**
 * Permissions System Setup Checker
 * Run this file to check if your database is ready for the permissions system
 */

include('connect.php');

echo "<!DOCTYPE html>
<html>
<head>
    <title>Permissions Setup Checker</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
        .success { color: #28a745; background: #d4edda; padding: 15px; border-radius: 5px; margin: 10px 0; }
        .error { color: #dc3545; background: #f8d7da; padding: 15px; border-radius: 5px; margin: 10px 0; }
        .warning { color: #856404; background: #fff3cd; padding: 15px; border-radius: 5px; margin: 10px 0; }
        .info { color: #004085; background: #cce5ff; padding: 15px; border-radius: 5px; margin: 10px 0; }
        h1 { color: #1a70b8; }
        code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; }
        .step { margin: 20px 0; padding: 15px; border-left: 4px solid #1a70b8; background: #f8f9fa; }
    </style>
</head>
<body>
    <h1>🔍 Permissions System Setup Checker</h1>";

// Check 1: Does tblclient table exist?
echo "<div class='step'><h3>Step 1: Checking tblclient table...</h3>";
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'tblclient'");
    if($stmt->rowCount() > 0) {
        echo "<div class='success'>✓ Table 'tblclient' exists</div>";
    } else {
        echo "<div class='error'>✗ Table 'tblclient' does not exist. Please run database_setup.sql first.</div>";
        exit;
    }
} catch(PDOException $e) {
    echo "<div class='error'>✗ Error checking table: " . $e->getMessage() . "</div>";
    exit;
}
echo "</div>";

// Check 2: Does userRole column exist?
echo "<div class='step'><h3>Step 2: Checking userRole column...</h3>";
try {
    $stmt = $pdo->query("SHOW COLUMNS FROM tblclient LIKE 'userRole'");
    if($stmt->rowCount() > 0) {
        echo "<div class='success'>✓ Column 'userRole' exists</div>";
        $hasUserRole = true;
    } else {
        echo "<div class='warning'>⚠ Column 'userRole' does not exist. Migration needed.</div>";
        $hasUserRole = false;
    }
} catch(PDOException $e) {
    echo "<div class='error'>✗ Error checking column: " . $e->getMessage() . "</div>";
    $hasUserRole = false;
}
echo "</div>";

// Check 3: Does permissions column exist?
echo "<div class='step'><h3>Step 3: Checking permissions column...</h3>";
try {
    $stmt = $pdo->query("SHOW COLUMNS FROM tblclient LIKE 'permissions'");
    if($stmt->rowCount() > 0) {
        echo "<div class='success'>✓ Column 'permissions' exists</div>";
        $hasPermissions = true;
    } else {
        echo "<div class='warning'>⚠ Column 'permissions' does not exist. Migration needed.</div>";
        $hasPermissions = false;
    }
} catch(PDOException $e) {
    echo "<div class='error'>✗ Error checking column: " . $e->getMessage() . "</div>";
    $hasPermissions = false;
}
echo "</div>";

// Check 4: Check current users
echo "<div class='step'><h3>Step 4: Checking existing users...</h3>";
try {
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM tblclient");
    $result = $stmt->fetch();
    echo "<div class='info'>ℹ Found " . $result['total'] . " user(s) in tblclient</div>";
    
    if($hasUserRole && $hasPermissions) {
        $stmt = $pdo->query("SELECT clientId, clientName, email, userRole, permissions FROM tblclient LIMIT 5");
        echo "<table border='1' cellpadding='10' style='width:100%; margin-top:10px; border-collapse: collapse;'>";
        echo "<tr style='background:#f8f9fa;'><th>ID</th><th>Name</th><th>Email</th><th>Role</th><th>Has Permissions</th></tr>";
        while($user = $stmt->fetch()) {
            $hasPerms = !empty($user['permissions']) ? 'Yes' : 'No';
            $role = $user['userRole'] ?? 'NULL';
            echo "<tr><td>{$user['clientId']}</td><td>{$user['clientName']}</td><td>{$user['email']}</td><td>{$role}</td><td>{$hasPerms}</td></tr>";
        }
        echo "</table>";
    }
} catch(PDOException $e) {
    echo "<div class='error'>✗ Error checking users: " . $e->getMessage() . "</div>";
}
echo "</div>";

// Final recommendation
echo "<div class='step'><h3>📋 Recommendation:</h3>";
if(!$hasUserRole || !$hasPermissions) {
    echo "<div class='warning'>
        <strong>⚠ Migration Required</strong><br><br>
        Your database needs to be updated to support the permissions system.<br><br>
        <strong>Run this SQL file:</strong><br>
        <code>client/database_migration_permissions.sql</code><br><br>
        You can run it using phpMyAdmin, MySQL Workbench, or command line:<br>
        <code>mysql -u username -p database_name < client/database_migration_permissions.sql</code>
    </div>";
} else {
    echo "<div class='success'>
        <strong>✓ Database is ready!</strong><br><br>
        Your database has all the required columns for the permissions system.<br>
        You can now access the Settings page and manage users.
    </div>";
}
echo "</div>";

echo "<div style='margin-top: 30px; padding: 20px; background: #e7f3ff; border-radius: 5px;'>
    <h3>🔗 Quick Links:</h3>
    <ul>
        <li><a href='login.php'>Login Page</a></li>
        <li><a href='dashboard.php'>Dashboard</a></li>
        <li><a href='settings.php'>Settings Page</a></li>
    </ul>
</div>";

echo "</body></html>";
?>
