<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header("Location: index");
    exit;
}
require_once 'db.php';

$user_id = $_SESSION['user_id'];

// Fetch full user details
$stmt = $conn->prepare("SELECT name, email,role, created_at FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();

// Fetch statistics for the profile
// Fetch count directly from database for accuracy and performance
if (($user['role'] ?? '') === 'Admin') {
    $stmt_count = $conn->prepare("SELECT COUNT(*) as count FROM projects");
} else {
    $stmt_count = $conn->prepare("SELECT COUNT(*) as count FROM projects WHERE created_by_user_id = ?");
    $stmt_count->bind_param("i", $user_id);
}
$stmt_count->execute();
$count_result = $stmt_count->get_result()->fetch_assoc();
$project_count = $count_result['count'] ?? 0;
$stmt_count->close();

// Get user initials for avatar
$user_name = $user['name'] ?? 'Admin User';
$initials = strtoupper(substr($user_name, 0, 1));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Profile - ConstructCRM</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #977C49;
            --primary-dark: #7a633a;
            --primary-light: #f8f5ef;
            --border-radius: 12px;
            --header-height: 70px;
            --nav-width: 260px;
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }
        body { font-family: 'Inter', sans-serif; background-color: #f5f7fa; }
        .header { background-color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.05); position: fixed; top: 0; left: 0; right: 0; height: var(--header-height); z-index: 1000; display: flex; align-items: center; padding: 0 20px; }
        .logo { font-weight: 700; font-size: 20px; color: var(--primary-color); display: flex; align-items: center; text-decoration: none;text-decoration: none; }
        .logo i { margin-right: 10px; font-size: 24px; }
        .mobile-menu-toggle { display: none; background: none; border: none; font-size: 24px; color: var(--primary-color); margin-right: 15px; cursor: pointer; }
        .main-container { display: flex; margin-top: var(--header-height); min-height: calc(100vh - var(--header-height)); }
        .sidebar-nav { width: var(--nav-width); background-color: #fff; box-shadow: 2px 0 10px rgba(0,0,0,0.05); position: fixed; top: var(--header-height); left: 0; bottom: 0; z-index: 999; transition: var(--transition); }
        .nav-link { display: flex; align-items: center; padding: 12px 20px; color: #555; text-decoration: none; font-weight: 500; }
        .nav-link.active { background-color: rgba(151, 124, 73, 0.1); color: var(--primary-color); border-left: 4px solid var(--primary-color); }
        .content-wrapper { flex: 1; margin-left: var(--nav-width); padding: 30px; transition: var(--transition); }
        .profile-card { background: #fff; border-radius: var(--border-radius); box-shadow: 0 4px 12px rgba(0,0,0,0.08); overflow: hidden; }
        .profile-header { background: linear-gradient(135deg, var(--primary-color), var(--primary-dark)); height: 120px; }
        .profile-body { padding: 0 30px 30px; position: relative; }
        .profile-avatar-wrapper { position: relative; margin-top: -60px; margin-bottom: 20px; }
        .profile-avatar { width: 120px; height: 120px; border-radius: 50%; background: #fff; border: 5px solid #fff; box-shadow: 0 4px 10px rgba(0,0,0,0.1); display: flex; align-items: center; justify-content: center; font-size: 48px; font-weight: 700; color: var(--primary-color); }
        .info-label { color: #999; font-size: 12px; text-transform: uppercase; font-weight: 600; margin-bottom: 5px; }
        .info-value { font-size: 16px; font-weight: 500; color: #333; margin-bottom: 20px; }
        .stat-badge { background: var(--primary-light); color: var(--primary-color); padding: 10px 20px; border-radius: 30px; display: inline-flex; align-items: center; gap: 10px; font-weight: 600; }
        
        .btn-primary { 
            background: linear-gradient(135deg, var(--primary-color), var(--primary-dark)); 
            border: none; padding: 10px 25px; border-radius: 10px; font-weight: 600; 
            box-shadow: 0 4px 12px rgba(151, 124, 73, 0.3);
            transition: var(--transition);
        }
        .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 15px rgba(151, 124, 73, 0.4); background: linear-gradient(135deg, var(--primary-dark), #5e4d2a); color: #fff; }

        /* Overlay for mobile menu */
        .overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 998; }
        .overlay.show { display: block; }

        @media (max-width: 768px) {
            .mobile-menu-toggle { display: block; }
            .sidebar-nav { transform: translateX(-100%); width: 280px; z-index: 1001; }
            .sidebar-nav.show { transform: translateX(0); }
            .content-wrapper { margin-left: 0; padding: 20px; }
            .header { padding: 0 15px; }
            .logo { font-size: 18px; }
            .logo i { font-size: 20px; }
        }
    </style>
</head>
<body>
    <header class="header">
        <button class="mobile-menu-toggle" id="mobileMenuToggle">
            <i class="bi bi-list"></i>
        </button>
        <a href="crmdashboard" class="logo"><img src="./assets/logo.png" alt="ConstructCRM Logo" style="height: 60px; width: 85px;"></a>    </header>
    <div class="main-container">
        <!-- Overlay for mobile -->
        <div class="overlay" id="overlay"></div>

        <nav class="sidebar-nav" id="sidebarNav">
            <div class="p-3">
                <div class="nav-title text-muted small fw-bold text-uppercase mb-2">Main</div>
                <a href="crmdashboard" class="nav-link"><i class="bi bi-speedometer2 me-2"></i> Dashboard</a>
                
                <a href="profile" class="nav-link active"><i class="bi bi-person me-2"></i> Profile</a>
                <a href="settings" class="nav-link"><i class="bi bi-gear me-2"></i> Settings</a>
                <hr>
                <a href="signin?logout=true" class="nav-link text-danger"><i class="bi bi-box-arrow-right me-2"></i> Logout</a>
            </div>
        </nav>
        <main class="content-wrapper">
            <div class="container-fluid">
                <h1 class="h3 mb-4 fw-bold">My Profile</h1>
                <div class="row">
                    <div class="col-lg-8">
                        <div class="profile-card">
                            <div class="profile-header"></div>
                            <div class="profile-body">
                                <div class="profile-avatar-wrapper">
                                    <div class="profile-avatar"><?php echo $initials; ?></div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="info-label">Full Name</div>
                                        <div class="info-value"><?php echo htmlspecialchars($user['name']); ?></div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="info-label">Email Address</div>
                                        <div class="info-value"><?php echo htmlspecialchars($user['email']); ?></div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="info-label">Member Since</div>
                                        <div class="info-value"><?php echo date('F d, Y', strtotime($user['created_at'])); ?></div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="info-label">Role</div>
                                        <div class="info-value"><?php echo htmlspecialchars($user['role'] ?? 'Pending Assignment'); ?></div>
                                    </div>
                                </div>
                                <hr class="my-4">
                                <div class="d-flex align-items-center justify-content-between">
                                    <div class="stat-badge">
                                        <i class="bi bi-briefcase-fill"></i>
                                        <span><?php echo $project_count; ?> Projects Managed</span>
                                    </div>
                                    <a href="settings" class="btn btn-primary">
                                        <i class="bi bi-pencil-square me-2"></i> Edit Account
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const mobileMenuToggle = document.getElementById('mobileMenuToggle');
            const sidebarNav = document.getElementById('sidebarNav');
            const overlay = document.getElementById('overlay');

            if (mobileMenuToggle && sidebarNav && overlay) {
                mobileMenuToggle.addEventListener('click', () => {
                    sidebarNav.classList.toggle('show');
                    overlay.classList.toggle('show');
                });

                overlay.addEventListener('click', () => {
                    sidebarNav.classList.remove('show');
                    overlay.classList.remove('show');
                });

                document.querySelectorAll('.nav-link').forEach(link => {
                    link.addEventListener('click', () => {
                        if (window.innerWidth <= 768) {
                            sidebarNav.classList.remove('show');
                            overlay.classList.remove('show');
                        }
                    });
                });
            }
        });
    </script>
</body>
</html>