<?php
session_start();
require_once __DIR__ . '/../../config/db.php';

if (!isset($_SESSION['user_id'])) {
	header("Location: ../../auth/login.php");
	exit;
}

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$role = $_SESSION['role'];

// Fetch user data
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) die("User not found.");

$extraData = [];
$projects = [];

// Role-based queries
if ($role === 'reporter') {
	$stmt = $pdo->prepare("SELECT * FROM tickets WHERE created_by = ?");
	$stmt->execute([$user_id]);
	$extraData = $stmt->fetchAll(PDO::FETCH_ASSOC);

	$stmt = $pdo->prepare("
		SELECT p.* FROM projects p
		JOIN project_memberships pm ON p.id = pm.project_id
		WHERE pm.user_id = ?
	");
	$stmt->execute([$user_id]);
	$projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if ($role === 'support') {
	$stmt = $pdo->prepare("SELECT * FROM tickets WHERE assigned_to = ?");
	$stmt->execute([$user_id]);
	$extraData = $stmt->fetchAll(PDO::FETCH_ASSOC);

	$stmt = $pdo->prepare("
		SELECT p.* FROM projects p
		JOIN project_memberships pm ON p.id = pm.project_id
		WHERE pm.user_id = ?
	");
	$stmt->execute([$user_id]);
	$projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if ($role === 'admin') {
	$extraData = [
		'users' => $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn(),
		'projects' => $pdo->query("SELECT COUNT(*) FROM projects")->fetchColumn(),
		'tickets' => $pdo->query("SELECT COUNT(*) FROM tickets")->fetchColumn(),
	];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile - <?= htmlspecialchars($user['full_name']); ?></title>
<link rel="icon" href="../assets/img/custora_logo.png" type="image/png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
:root {
	--bg:#f4f7fa; --card-bg:rgba(173,216,230,0.35);
	--accent:#005f8d; --muted:#666; --text:#1b2430; --white:#fff;
}
* {box-sizing:border-box;margin:0;padding:0;}
body {font-family:Inter,"Segoe UI",Roboto,Arial,sans-serif;background:var(--bg);color:var(--text);overflow-x:hidden;}
a {text-decoration:none;color:inherit;}
.app {display:flex;min-height:100vh;}
.sidebar {
	width:250px;background:skyblue;color:#003547;position:fixed;left:0;top:0;height:100vh;
	overflow:auto;z-index:200;transition:transform .3s ease-in-out;
}
.sidebar .logo {text-align:center;margin:15px 0;}
.sidebar .logo img {width:180px;height:auto;margin-top:20px;}
.nav {margin-top:12px;padding-left:10px;}
.nav a {
	display:block;padding:10px 14px;color:#003547;border-radius:8px;margin-bottom:6px;font-weight:600;
}
.nav a:hover {
	background:rgba(255,255,255,0.8);
	box-shadow:0 2px 6px rgba(0,0,0,0.06);
}
.footer-note {
	text-align:center;font-size:13px;padding:10px;color:var(--muted);
}
.main {
	margin-left:260px;padding:20px;width:100%;transition:margin-left .25s;
}
.header {
	display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;margin-bottom:20px;
}
.header-left {display:flex;align-items:center;gap:10px;}
.hamburger {
	display:none;background:var(--accent);color:white;padding:9px 11px;border-radius:8px;cursor:pointer;
	font-size:20px;
}
.title {font-weight:700;font-size:1.3rem;color:#0b66d1;}
.controls {display:flex;align-items:center;gap:10px;}
button#darkToggle {
	padding:8px 10px;border:none;border-radius:8px;background:var(--card-bg);cursor:pointer;
}
.card {
	background:var(--card-bg);padding:25px;border-radius:12px;
	box-shadow:0 4px 10px rgba(0,0,0,0.08);margin-bottom:20px;
}
.card h2 {margin-bottom:10px;color:var(--accent);}
.card p,strong {color:var(--text);}
ul {margin-top:10px;margin-left:20px;}
ul li {margin-bottom:5px;}
.overlay {
	display:none;position:fixed;top:0;left:0;width:100%;height:100%;
	background:rgba(0,0,0,0.4);z-index:150;
}
.sidebar.active {transform:translateX(0);}
@media(max-width:768px) {
	.sidebar {transform:translateX(-280px);}
	.sidebar.active ~ .overlay {display:block;}
	.main {margin-left:0;padding:12px;}
	.hamburger {display:inline-block;}
}
body.dark {
	--bg:#081014;--card-bg:rgba(255,255,255,0.04);--accent:#1fb6ff;
	--muted:#9aa6af;--text:#e6f2f6;--white:#0a0a0a;
}
body.dark .sidebar {background:linear-gradient(180deg,#0b2b33,#072024);color:#bfeafc;}
body.dark .nav a {color:#bfeafc;}
body.dark .title {color:#1fb6ff;}
</style>
</head>
<body>
<div class="app">
	<aside class="sidebar" id="sidebar">
		<div class="logo"><img src="../assets/img/custora_logo.png" alt="Custora CRM"></div>
		<nav class="nav">
			<a href="../"><i class="fas fa-gauge"></i> Dashboard</a>
			<?php if ($role === 'reporter'): ?>
				<a href="../projects/"><i class="fas fa-folder-open"></i> My Projects</a>
				<a href="../issues/"><i class="fas fa-bug"></i> Report Issue</a>
			<?php elseif ($role === 'support'): ?>
				<a href="../issues/"><i class="fas fa-tasks"></i> Assigned Issues</a>
				<a href="../projects/"><i class="fas fa-handshake-angle"></i> Support Projects</a>
			<?php elseif ($role === 'admin'): ?>
				<a href="../projects/"><i class="fas fa-diagram-project"></i> Manage Projects</a>
				<a href="../users/"><i class="fas fa-users-cog"></i> Manage Users</a>
				<a href="../issues/"><i class="fas fa-circle-exclamation"></i> All Issues</a>
			<?php endif; ?>
			<a href="../profile/"><i class="fas fa-user"></i> My Profile</a>
			<a href="../../auth/logout.php" style="color:#b31b1b;"><i class="fas fa-sign-out-alt"></i> Logout</a>
		</nav>
		<div class="footer-note">&copy; <?= date('Y') ?> Digital Vortex Technologies</div>
	</aside>

	<div class="overlay" id="overlay" onclick="toggleSidebar()"></div>

	<main class="main">
		<div class="header">
			<div class="header-left">
				<div class="hamburger" id="hamburger" onclick="toggleSidebar()"><i class="fas fa-bars"></i></div>
				<div class="title">Profile</div>
			</div>
			<div class="controls">
				<div style="font-size:0.9rem;color:var(--muted);">
					Welcome, <strong><?= htmlspecialchars($username) ?></strong><br>
					<small>Role: <?= htmlspecialchars($role) ?></small>
				</div>
				<button id="darkToggle"><span id="darkLabel">Dark</span></button>
			</div>
		</div>

		<div class="card">
			<h2><?= htmlspecialchars($user['full_name']); ?></h2>
			<p><strong>Username:</strong> <?= htmlspecialchars($user['username']); ?></p>
			<p><strong>Email:</strong> <?= htmlspecialchars($user['email']); ?></p>
			<p><strong>Role:</strong> <?= htmlspecialchars($user['role']); ?></p>
			<p><strong>Status:</strong> <?= htmlspecialchars($user['status']); ?></p>
			<p><strong>Joined:</strong> <?= htmlspecialchars($user['created_at']); ?></p>
		</div>

		<?php if ($role === 'reporter'): ?>
			<div class="card">
				<h2>My Reported Tickets</h2>
				<?php if ($projects): ?>
					<h3>My Project Groups:</h3>
					<ul>
						<?php foreach ($projects as $proj): ?>
							<li><?= htmlspecialchars($proj['name']); ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>

				<?php if ($extraData): ?>
					<h3>Tickets:</h3>
					<ul>
						<?php foreach ($extraData as $ticket): ?>
							<li>#<?= $ticket['id']; ?> - <?= htmlspecialchars($ticket['title']); ?> (<?= htmlspecialchars($ticket['status']); ?>)</li>
						<?php endforeach; ?>
					</ul>
				<?php else: ?>
					<p>No tickets reported yet.</p>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php if ($role === 'support'): ?>
			<div class="card">
				<h2>Tickets Assigned to Me</h2>
				<?php if ($projects): ?>
					<h3>My Project Groups:</h3>
					<ul>
						<?php foreach ($projects as $proj): ?>
							<li><?= htmlspecialchars($proj['name']); ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>

				<?php if ($extraData): ?>
					<ul>
						<?php foreach ($extraData as $ticket): ?>
							<li>#<?= $ticket['id']; ?> - <?= htmlspecialchars($ticket['title']); ?> (<?= htmlspecialchars($ticket['status']); ?>)</li>
						<?php endforeach; ?>
					</ul>
				<?php else: ?>
					<p>No tickets assigned yet.</p>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php if ($role === 'admin'): ?>
			<div class="card">
				<h2>System Overview</h2>
				<p><strong>Total Users:</strong> <?= $extraData['users']; ?></p>
				<p><strong>Total Projects:</strong> <?= $extraData['projects']; ?></p>
				<p><strong>Total Tickets:</strong> <?= $extraData['tickets']; ?></p>
			</div>
		<?php endif; ?>
	</main>
</div>

<script>
function toggleSidebar() {
	const sidebar = document.getElementById('sidebar');
	const overlay = document.getElementById('overlay');
	sidebar.classList.toggle('active');
	overlay.style.display = sidebar.classList.contains('active') ? 'block' : 'none';
}
(function(){
	const body=document.body,toggle=document.getElementById('darkToggle'),label=document.getElementById('darkLabel');
	if(localStorage.getItem('custora_dark')==='1'){body.classList.add('dark');label.textContent='Light';}
	toggle.addEventListener('click',()=>{
		body.classList.toggle('dark');
		localStorage.setItem('custora_dark',body.classList.contains('dark')?'1':'0');
		label.textContent=body.classList.contains('dark')?'Light':'Dark';
	});
})();
</script>
</body>
</html>