<?php
/**
 * Enterprise HRM ERP System - Check Admin User
 * Diagnostic script to check if admin user exists
 */

require_once __DIR__ . '/../includes/init.php';

echo "<!DOCTYPE html>
<html>
<head>
    <title>Check Admin User</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 40px; background: #f5f5f5; }
        .box { background: white; padding: 30px; border-radius: 8px; max-width: 800px; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #1e293b; }
        .success { background: #d1fae5; padding: 15px; border-radius: 6px; margin: 10px 0; border-left: 4px solid #10b981; }
        .error { background: #fee2e2; padding: 15px; border-radius: 6px; margin: 10px 0; border-left: 4px solid #ef4444; }
        .info { background: #dbeafe; padding: 15px; border-radius: 6px; margin: 10px 0; border-left: 4px solid #3b82f6; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #e2e8f0; }
        th { background: #f1f5f9; font-weight: 600; }
        .btn { display: inline-block; padding: 12px 24px; background: #1e293b; color: white; text-decoration: none; border-radius: 6px; margin: 10px 5px 0 0; }
        .btn:hover { background: #1e40af; }
        .btn-danger { background: #ef4444; }
        .btn-danger:hover { background: #dc2626; }
    </style>
</head>
<body>
    <div class='box'>
        <h1>Admin User Diagnostic</h1>";

try {
    $db = getDB();
    
    // Check database connection
    echo "<div class='success'>✓ Database connection successful</div>";
    
    // Check if users table exists
    $stmt = $db->query("SHOW TABLES LIKE 'users'");
    if ($stmt->rowCount() == 0) {
        echo "<div class='error'>✗ Users table does not exist. Please import database/schema.sql first!</div>";
        echo "<a href='" . BASE_URL . "/database/schema.sql' class='btn'>Download Schema</a>";
        echo "</div></body></html>";
        exit;
    }
    
    echo "<div class='success'>✓ Users table exists</div>";
    
    // Check for admin users
    $stmt = $db->query("SELECT COUNT(*) FROM users WHERE role_id = 1");
    $adminCount = $stmt->fetchColumn();
    
    if ($adminCount == 0) {
        echo "<div class='error'>✗ No admin user found!</div>";
        echo "<div class='info'>You need to create an admin user first.</div>";
        echo "<a href='" . BASE_URL . "/setup/create_admin_direct.php' class='btn'>Create Admin User Now</a>";
    } else {
        echo "<div class='success'>✓ Found {$adminCount} admin user(s)</div>";
        
        // List all users
        $stmt = $db->query("
            SELECT u.user_id, u.username, u.email, u.full_name, u.user_status, r.role_name
            FROM users u
            LEFT JOIN roles r ON u.role_id = r.role_id
            ORDER BY u.user_id
        ");
        $users = $stmt->fetchAll();
        
        echo "<h2>All Users in Database:</h2>";
        echo "<table>";
        echo "<tr><th>ID</th><th>Username</th><th>Email</th><th>Full Name</th><th>Role</th><th>Status</th></tr>";
        
        foreach ($users as $user) {
            $statusClass = $user['user_status'] === 'active' ? 'success' : 'error';
            echo "<tr>";
            echo "<td>{$user['user_id']}</td>";
            echo "<td><strong>{$user['username']}</strong></td>";
            echo "<td>{$user['email']}</td>";
            echo "<td>{$user['full_name']}</td>";
            echo "<td>{$user['role_name']}</td>";
            echo "<td><span class='{$statusClass}'>{$user['user_status']}</span></td>";
            echo "</tr>";
        }
        
        echo "</table>";
        
        // Test password
        echo "<h2>Test Login Credentials:</h2>";
        echo "<div class='info'>";
        echo "<strong>Try these credentials:</strong><br>";
        
        $stmt = $db->query("SELECT username, email FROM users WHERE role_id = 1 LIMIT 1");
        $admin = $stmt->fetch();
        
        if ($admin) {
            echo "Username: <strong>{$admin['username']}</strong><br>";
            echo "Email: <strong>{$admin['email']}</strong><br>";
            echo "<br>If password doesn't work, you may need to reset it.";
        }
        echo "</div>";
        
        echo "<a href='" . BASE_URL . "/login.php' class='btn'>Go to Login</a>";
        echo "<a href='" . BASE_URL . "/setup/reset_password.php?username={$admin['username']}' class='btn btn-danger'>Reset Password</a>";
    }
    
    // Check sites
    $stmt = $db->query("SELECT COUNT(*) FROM sites");
    $siteCount = $stmt->fetchColumn();
    if ($siteCount == 0) {
        echo "<div class='error'>✗ No sites found. Default site should be created.</div>";
    } else {
        echo "<div class='success'>✓ Found {$siteCount} site(s)</div>";
    }
    
} catch (Exception $e) {
    echo "<div class='error'>✗ Error: " . htmlspecialchars($e->getMessage()) . "</div>";
    echo "<div class='info'>Please check your database configuration in config/config.php</div>";
}

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

