<?php
session_start();

if (!isset($_SESSION["user"]) || empty($_SESSION["user"]["is_admin"])) {
    header("Location: ../Index.php");
    exit;
}

require "../Auth/db.php";

$userId = $_GET["id"] ?? null;

if (!$userId) {
    die("Missing user ID.");
}

/* LOAD ALL BADGES */

$badgeStmt = $pdo->query("
    SELECT *
    FROM user_badges
    ORDER BY badge_name ASC
");

$allBadges = $badgeStmt->fetchAll(PDO::FETCH_ASSOC);

/* ASSIGN BADGE */

if (isset($_POST["assign_badge"])) {

    $badgeId = (int)($_POST["badge_id"] ?? 0);

    if ($badgeId > 0) {

        $checkStmt = $pdo->prepare("
            SELECT id
            FROM user_assigned_badges
            WHERE user_id = ? AND badge_id = ?
        ");

        $checkStmt->execute([
            $userId,
            $badgeId
        ]);

        if (!$checkStmt->fetch()) {

            $insertStmt = $pdo->prepare("
                INSERT INTO user_assigned_badges
                (user_id, badge_id)
                VALUES (?, ?)
            ");

            $insertStmt->execute([
                $userId,
                $badgeId
            ]);
        }
    }

    header("Location: UserProfile.php?id=" . $userId);
    exit;
}

/* REMOVE BADGE */

if (isset($_GET["remove_badge"])) {

    $removeBadgeId = (int)$_GET["remove_badge"];

    $stmt = $pdo->prepare("
        DELETE FROM user_assigned_badges
        WHERE user_id = ? AND badge_id = ?
    ");

    $stmt->execute([
        $userId,
        $removeBadgeId
    ]);

    header("Location: UserProfile.php?id=" . $userId);
    exit;
}

/* GET USER BADGES */

$userBadgeStmt = $pdo->prepare("
    SELECT ub.*
    FROM user_assigned_badges uab
    INNER JOIN user_badges ub
        ON uab.badge_id = ub.id
    WHERE uab.user_id = ?
    ORDER BY uab.id DESC
");

$userBadgeStmt->execute([$userId]);

$userBadges = $userBadgeStmt->fetchAll(PDO::FETCH_ASSOC);

$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$userId]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    die("User not found.");
}

$avatar = "../Assets/Logo.png";

if (!empty($user["avatar"]) && !empty($user["discord_id"])) {
    $avatar = "https://cdn.discordapp.com/avatars/" .
        $user["discord_id"] . "/" .
        $user["avatar"] . ".png";
}

$isVip = !empty($user["is_vip"]) ? "Yes" : "No";
$isBanned = !empty($user["is_banned"]) ? "Yes" : "No";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Profile - Rewire Admin</title>
<link rel="icon" type="image/png" href="../Assets/Logo.png">
<script src="https://unpkg.com/lucide@latest"></script>
<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
}

body {
    background: #050505;
    color: white;
}

/* PAGE WRAP */
.profile-page {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 50px 25px;
}

/* PROFILE WRAPPER */
.profile-wrapper {
    width: 100%;
    max-width: 850px;
    background: #071014;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 20px;
    padding: 30px;
    box-shadow: 0 0 35px rgba(0,194,255,0.05);
}

/* BACK BUTTON */
.back-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 28px;
    color: #00c2ff;
    text-decoration: none;
    font-weight: 700;
    transition: 0.25s ease;
}

.back-btn:hover {
    opacity: 0.8;
}

/* TOP PROFILE */
.profile-top {
    display: flex;
    align-items: center;
    gap: 24px;
    margin-bottom: 30px;
}

.profile-avatar {
    width: 100px;
    height: 100px;
    border-radius: 24px;
    object-fit: cover;
    border: 2px solid rgba(0,194,255,0.25);
    box-shadow: 0 0 18px rgba(0,194,255,0.15);
}

.profile-name {
    font-size: 2.4rem;
    font-weight: 800;
    color: white;
}

.profile-email {
    color: #7d8894;
    margin-top: 6px;
    font-size: 0.95rem;
}

/* INFO GRID */
.info-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 18px;
    margin-bottom: 28px;
}

.info-card {
    background: #050505;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 16px;
    padding: 20px;
    transition: 0.25s ease;
}

.info-card:hover {
    border-color: rgba(0,194,255,0.28);
    box-shadow: 0 0 18px rgba(0,194,255,0.08);
}

.info-label {
    color: #7d8894;
    font-size: 0.74rem;
    font-weight: 800;
    letter-spacing: 1px;
    margin-bottom: 10px;
}

.info-value {
    font-size: 1.05rem;
    font-weight: 700;
    color: white;
}

/* STATUS COLORS */
.vip-yes {
    color: #00c2ff;
}

.vip-no {
    color: #ff5e5e;
}

.banned-yes {
    color: #ff4b4b;
}

.banned-no {
    color: #00d26a;
}

/* FORM PANEL */
.profile-form {
    background: #050505;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 16px;
    padding: 24px;
}

.form-title {
    font-size: 1.1rem;
    font-weight: 800;
    margin-bottom: 22px;
}

/* FORM GROUP */
.form-group {
    margin-bottom: 22px;
}

.form-group label {
    display: block;
    margin-bottom: 10px;
    color: #c8d0d8;
    font-size: 0.92rem;
    font-weight: 700;
}

/* SELECT DROPDOWN FIX */
.form-group select {
    width: 100%;
    height: 54px;
    background: #071014;
    color: white;
    border: 1px solid rgba(0,194,255,0.18);
    border-radius: 12px;
    padding: 0 18px;
    font-size: 0.95rem;
    outline: none;

    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;

    transition: 0.25s ease;

    background-image:
        linear-gradient(45deg, transparent 50%, #00c2ff 50%),
        linear-gradient(135deg, #00c2ff 50%, transparent 50%);
    background-position:
        calc(100% - 22px) calc(50% - 3px),
        calc(100% - 16px) calc(50% - 3px);
    background-size: 6px 6px;
    background-repeat: no-repeat;
}

.form-group select:hover {
    border-color: rgba(0,194,255,0.35);
}

.form-group select:focus {
    border-color: #00c2ff;
    box-shadow: 0 0 18px rgba(0,194,255,0.2);
}

/* BUTTON */
.save-btn {
    height: 52px;
    padding: 0 24px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, #00c2ff, #0090c7);
    color: white;
    font-weight: 800;
    font-size: 0.95rem;
    cursor: pointer;
    transition: 0.25s ease;
}

.save-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(0,194,255,0.35);
}

/* RESPONSIVE */
@media (max-width: 768px) {

    .profile-wrapper {
        padding: 22px;
    }

    .profile-top {
        flex-direction: column;
        align-items: flex-start;
    }

    .info-grid {
        grid-template-columns: 1fr;
    }

    .profile-name {
        font-size: 2rem;
    }
}

.profile-page-wrapper {
    width: 100%;
    max-width: 950px;
    margin: 50px auto;
    padding: 0 25px;
}

.profile-page {
    width: 100%;
    display: flex;
    justify-content: center;
}

.back-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 22px;
    color: #00c2ff;
    text-decoration: none;
    font-weight: 700;
    transition: 0.25s ease;
}

.back-btn:hover {
    opacity: 0.8;
    transform: translateX(-3px);
}
</style>
</head>
<body>
<?php include "../Preloader.php"; ?>
<div class="profile-page-wrapper">

    <a href="Dashboard.php" class="back-btn">
        <i data-lucide="arrow-left"></i>
        Back To Dashboard
    </a>

    <div class="profile-page">

    <div class="profile-wrapper">
    
        <div class="profile-top">
            <img src="<?php echo htmlspecialchars($avatar); ?>" class="profile-avatar">
    
            <div>
                <div class="profile-name">
                    <?php echo htmlspecialchars($user["username"]); ?>
                </div>
    
                <div class="profile-email">
                    <?php echo htmlspecialchars($user["email"] ?? "No email connected"); ?>
                </div>
            </div>
        </div>
    
        <div class="info-grid">
    
            <div class="info-card">
                <div class="info-label">DISCORD IDENTIFIER</div>
                <div class="info-value">
                    <?php echo htmlspecialchars($user["discord_id"] ?? "Email Account"); ?>
                </div>
            </div>
    
            <div class="info-card">
                <div class="info-label">PROVIDER</div>
                <div class="info-value">
                    <?php echo htmlspecialchars($user["provider"] ?? "Unknown"); ?>
                </div>
            </div>
    
            <div class="info-card">
                <div class="info-label">VIP STATUS</div>
                <div class="info-value <?php echo !empty($user["is_vip"]) ? 'vip-yes' : 'vip-no'; ?>">
                    <?php echo $isVip; ?>
                </div>
            </div>
    
            <div class="info-card">
                <div class="info-label">BANNED</div>
                <div class="info-value <?php echo !empty($user["is_banned"]) ? 'banned-yes' : 'banned-no'; ?>">
                    <?php echo $isBanned; ?>
                </div>
            </div>
    
        </div>
    
        <form class="profile-form" method="POST">
            <input type="hidden" name="id" value="<?php echo htmlspecialchars($user["id"]); ?>">
    
            <div class="form-title">
                Account Controls
            </div>
    
            <div class="form-group">
                <label>VIP Status</label>
    
                <select name="is_vip">
                    <option value="0" <?php echo empty($user["is_vip"]) ? "selected" : ""; ?>>
                        No
                    </option>
    
                    <option value="1" <?php echo !empty($user["is_vip"]) ? "selected" : ""; ?>>
                        Yes
                    </option>
                </select>
            </div>
    
            <div class="form-group">
                <label>Ban From Website</label>
    
                <select name="is_banned">
                    <option value="0" <?php echo empty($user["is_banned"]) ? "selected" : ""; ?>>
                        No
                    </option>
    
                    <option value="1" <?php echo !empty($user["is_banned"]) ? "selected" : ""; ?>>
                        Yes
                    </option>
                </select>
            </div>
            
            <div class="form-group">
                <label>Assign Badge</label>
            
                <select name="badge_id">
                    <option value="">
                        Select Badge
                    </option>
            
                    <?php foreach ($allBadges as $badge): ?>
            
                        <option value="<?php echo $badge["id"]; ?>">
                            <?php echo htmlspecialchars($badge["badge_name"]); ?>
                        </option>
            
                    <?php endforeach; ?>
                </select>
            </div>
            
            <button
                class="save-btn"
                type="submit"
                name="assign_badge"
                style="margin-bottom:20px;"
            >
                Assign Badge
            </button>            
    
            <button class="save-btn" type="submit">
                Save Changes
            </button>
    
        </form>
        <div class="profile-form" style="margin-top:25px;">
            <div class="form-title">
                Assigned Badges
            </div>
            <?php if (empty($userBadges)): ?>
        
                <div class="info-value">
                    No Badges Assigned.
                </div>
        
            <?php endif; ?>
        
            <div
                style="
                    display:grid;
                    grid-template-columns:repeat(auto-fill,minmax(180px,1fr));
                    gap:18px;
                "
            >
        
                <?php foreach ($userBadges as $badge): ?>
        
                    <div
                        style="
                            background:#071014;
                            border:1px solid rgba(0,194,255,0.12);
                            border-radius:16px;
                            padding:18px;
                            text-align:center;
                        "
                    >
        
                        <img
                            src="../<?php echo htmlspecialchars($badge["badge_image"]); ?>"
                            style="
                                width:100%;
                                max-height:90px;
                                object-fit:contain;
                                margin-bottom:14px;
                            "
                        >
        
                        <div
                            style="
                                font-weight:800;
                                margin-bottom:12px;
                            "
                        >
                            <?php echo htmlspecialchars($badge["badge_name"]); ?>
                        </div>
        
                        <a
                            href="UserProfile.php?id=<?php echo $userId; ?>&remove_badge=<?php echo $badge["id"]; ?>"
                            style="
                                display:inline-flex;
                                align-items:center;
                                justify-content:center;
                                width:100%;
                                height:42px;
                                border-radius:10px;
                                text-decoration:none;
                                background:rgba(255,59,59,0.12);
                                border:1px solid rgba(255,59,59,0.25);
                                color:#ff6b6b;
                                font-weight:800;
                            "
                        >
                            Remove
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>

<script>
lucide.createIcons();
</script>

</body>
</html>