<?php
/**
 * Agentic Square - Client Profile Update Console
 * Path: client/update-profile.php
 */

require_once __DIR__ . '/../config/config.php';

if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] ?? '') === 'admin' || ($_SESSION['user_role'] ?? '') === 'seller') {
    header('Location: ../login.php');
    exit;
}

$user_id = (int)$_SESSION['user_id'];
$success_message = '';
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $full_name = trim($_POST['full_name'] ?? '');
    $email = filter_var(trim($_POST['email'] ?? ''), FILTER_VALIDATE_EMAIL);

    if ($full_name === '' || !$email) {
        $error_message = 'Please provide a valid name and email address.';
    } else {
        try {
            $stmt = $pdo->prepare("UPDATE users SET full_name = ?, email = ?, updated_at = NOW() WHERE id = ?");
            $stmt->execute([$full_name, $email, $user_id]);

            $_SESSION['user_name'] = $full_name;
            $_SESSION['user_email'] = $email;
            $success_message = 'Profile updated successfully.';
        } catch (Throwable $e) {
            $error_message = 'Profile update failed: ' . $e->getMessage();
        }
    }
}

$stmt = $pdo->prepare("SELECT full_name, email FROM users WHERE id = ? LIMIT 1");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];

$page_title = 'Update Profile';
require_once __DIR__ . '/../sections/header.php';
?>

<body class="bg-nftDark text-white antialiased overflow-x-hidden">
    <?php include_once __DIR__ . '/client-sidebar.php'; ?>

    <main class="lg:ml-64 min-h-screen pt-24 lg:pt-10 pb-16 px-4 sm:px-6 lg:px-8">
        <div class="max-w-3xl mx-auto space-y-8">
            <div class="border-b border-slate-900/60 pb-6">
                <span class="text-xs font-bold tracking-widest text-nftAccentCyan uppercase block mb-1">// Account Identity</span>
                <h1 class="text-3xl font-extrabold tracking-tight text-white">Update Profile</h1>
                <p class="text-sm text-nftTextGray mt-1">Manage your client account name and contact email.</p>
            </div>

            <?php if ($success_message): ?>
                <div class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 p-4 rounded-xl text-xs font-mono">
                    <?php echo htmlspecialchars($success_message, ENT_QUOTES, 'UTF-8'); ?>
                </div>
            <?php endif; ?>

            <?php if ($error_message): ?>
                <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-xs font-mono">
                    <?php echo htmlspecialchars($error_message, ENT_QUOTES, 'UTF-8'); ?>
                </div>
            <?php endif; ?>

            <form method="POST" class="glass-card p-6 rounded-2xl border border-slate-800/80 bg-slate-950/20 space-y-6">
                <div class="space-y-2">
                    <label for="full_name" class="text-xs font-bold uppercase text-nftTextGray">Full Name</label>
                    <input type="text" id="full_name" name="full_name" required value="<?php echo htmlspecialchars($user['full_name'] ?? '', ENT_QUOTES, 'UTF-8'); ?>" class="w-full bg-slate-950 p-3 rounded-xl border border-slate-800 focus:border-nftAccentCyan outline-none transition-all">
                </div>

                <div class="space-y-2">
                    <label for="email" class="text-xs font-bold uppercase text-nftTextGray">Email Address</label>
                    <input type="email" id="email" name="email" required value="<?php echo htmlspecialchars($user['email'] ?? '', ENT_QUOTES, 'UTF-8'); ?>" class="w-full bg-slate-950 p-3 rounded-xl border border-slate-800 focus:border-nftAccentCyan outline-none transition-all">
                </div>

                <button type="submit" class="w-full py-4 rounded-xl bg-gradient-to-r from-nftAccentCyan to-nftAccentPurple font-bold text-white uppercase tracking-widest hover:opacity-90 transition-all shadow-lg">
                    Save Profile
                </button>
            </form>
        </div>
    </main>
</body>
</html>
