<?php
/**
 * Agentic Square - Premium Seller Dashboard: Account Security
 * Path: seller/account-settings.php
 */

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

// SECURITY ACCESS GUARD
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit;
}

$user_id = (int)$_SESSION['user_id'];
$message = "";
$error = "";

// HANDLE ACCOUNT UPDATES
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    // Update Email
    if ($action === 'update_email') {
        $new_email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
        if ($new_email) {
            $stmt = $pdo->prepare("UPDATE users SET email = ?, updated_at = NOW() WHERE id = ?");
            $stmt->execute([$new_email, $user_id]);
            $message = "Email address updated successfully.";
        } else {
            $error = "Invalid email format.";
        }
    } 
    // Update Password
    elseif ($action === 'update_password') {
        $current_pass = $_POST['current_password'] ?? '';
        $new_pass     = $_POST['new_password'] ?? '';
        
        $stmt = $pdo->prepare("SELECT password FROM users WHERE id = ?");
        $stmt->execute([$user_id]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if (password_verify($current_pass, $user['password'])) {
            $hashed_password = password_hash($new_pass, PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("UPDATE users SET password = ?, updated_at = NOW() WHERE id = ?");
            $stmt->execute([$hashed_password, $user_id]);
            $message = "Password updated successfully.";
        } else {
            $error = "Current password verification failed.";
        }
    }
}

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

require_once __DIR__ . '/../sections/header.php';
?>

<body class="bg-nftDark text-white antialiased">
    <?php include_once __DIR__ . '/seller-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-4xl mx-auto space-y-8">
            <div class="border-b border-slate-900/60 pb-6">
                <h1 class="text-3xl font-extrabold tracking-tight text-white">Account Settings</h1>
                <p class="text-sm text-nftTextGray mt-1">Manage your login credentials and secure your marketplace access.</p>
            </div>

            <?php if($message): ?>
                <div class="bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 p-4 rounded-xl text-xs font-bold font-mono">
                    <i class="fa-solid fa-circle-check mr-2"></i> <?php echo $message; ?>
                </div>
            <?php endif; ?>
            <?php if($error): ?>
                <div class="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-4 rounded-xl text-xs font-bold font-mono">
                    <i class="fa-solid fa-triangle-exclamation mr-2"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form method="POST" class="glass-card p-8 rounded-2xl border border-slate-800/80 bg-slate-950/20 space-y-4">
                <input type="hidden" name="action" value="update_email">
                <h2 class="text-lg font-bold text-white mb-2">Login Email</h2>
                <div class="grid md:grid-cols-2 gap-4">
                    <input type="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" class="w-full bg-slate-950/80 border border-slate-900 rounded-xl p-3.5 text-sm text-white focus:border-nftAccentCyan outline-none">
                    <button type="submit" class="bg-slate-900 hover:bg-slate-800 text-white font-bold text-xs px-6 rounded-xl transition-all border border-slate-800">
                        Update Email
                    </button>
                </div>
            </form>

            <form method="POST" class="glass-card p-8 rounded-2xl border border-slate-800/80 bg-slate-950/20 space-y-4">
                <input type="hidden" name="action" value="update_password">
                <h2 class="text-lg font-bold text-white mb-2">Security Credentials</h2>
                <div class="grid md:grid-cols-3 gap-4">
                    <input type="password" name="current_password" placeholder="Current Password" required class="w-full bg-slate-950/80 border border-slate-900 rounded-xl p-3.5 text-sm text-white outline-none">
                    <input type="password" name="new_password" placeholder="New Password" required class="w-full bg-slate-950/80 border border-slate-900 rounded-xl p-3.5 text-sm text-white outline-none">
                    <button type="submit" class="bg-gradient-to-r from-nftAccentCyan to-nftAccentPurple text-slate-950 font-black tracking-wide text-xs px-6 rounded-xl shadow-lg hover:opacity-90 transition-all">
                        Update Password
                    </button>
                </div>
            </form>
        </div>
    </main>
</body>
</html>