<?php
declare(strict_types=1);
session_start();

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

require_once 'db/config.php';

$userId = $_SESSION['user_id'];
$errors = [];
$messages = [];

// Fetch user data once to use for POST and for initial display
try {
    $pdo = db();
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->execute([$userId]);
    $user = $stmt->fetch();
    if (!$user) {
        session_destroy();
        header('Location: login.php');
        exit;
    }
} catch (PDOException $e) {
    die('Could not fetch user data: ' . $e->getMessage());
}

// Handle profile update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $firstName = trim($_POST['first_name'] ?? '');
    $lastName = trim($_POST['last_name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $phoneNumber = trim($_POST['phone_number'] ?? '');
    $riderLevel = trim($_POST['rider_level'] ?? '');
    $avatarPath = $user['avatar'] ?? ''; // Start with existing avatar

    // --- Validation ---
    if (empty($firstName)) {
        $errors['first_name'] = 'First name is required.';
    }

    if (empty($lastName)) {
        $errors['last_name'] = 'Last name is required.';
    }

    if (empty($email)) {
        $errors['email'] = 'Email is required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = 'Please enter a valid email address.';
    }

    // Handle avatar upload
    if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
        $uploadDir = 'assets/avatars/';
        if (!is_dir($uploadDir)) {
            if (!mkdir($uploadDir, 0755, true)) {
                $errors['avatar'] = 'Failed to create avatar directory.';
            }
        }

        if (!isset($errors['avatar'])) {
            $avatarTmpPath = $_FILES['avatar']['tmp_name'];
            $avatarName = basename($_FILES['avatar']['name']);
            $avatarSize = $_FILES['avatar']['size'];
            $avatarExtension = strtolower(pathinfo($avatarName, PATHINFO_EXTENSION));
            $allowedExtensions = ['jpg', 'jpeg', 'png'];
            $maxFileSize = 2 * 1024 * 1024; // 2 MB

            if (!in_array($avatarExtension, $allowedExtensions)) {
                $errors['avatar'] = 'Invalid file type. Only JPG and PNG are allowed.';
            } elseif ($avatarSize > $maxFileSize) {
                $errors['avatar'] = 'File is too large. Maximum size is 2MB.';
            } else {
                $safeAvatarName = uniqid('avatar_', true) . '.' . $avatarExtension;
                $newAvatarPath = $uploadDir . $safeAvatarName;

                if (move_uploaded_file($avatarTmpPath, $newAvatarPath)) {
                    // Remove the old avatar if it exists and is not a default one
                    if (!empty($avatarPath) && file_exists($avatarPath)) {
                        unlink($avatarPath);
                    }
                    $avatarPath = $newAvatarPath; // Set new path for DB update
                } else {
                    $errors['avatar'] = 'Failed to upload new avatar.';
                }
            }
        }
    }

    // Phone number validation
    if (!empty($phoneNumber)) {
        $numericPhoneNumber = preg_replace('/[^0-9]/', '', $phoneNumber);
        if (strlen($numericPhoneNumber) < 7) {
            $errors['phone_number'] = 'Phone number must be at least 7 digits.';
        } else {
            $phoneNumber = $numericPhoneNumber; // Use the sanitized number
        }
    }

    if (empty($errors)) {
        try {
            $stmt = $pdo->prepare(
                'UPDATE users SET first_name = ?, last_name = ?, email = ?, phone_number = ?, rider_level = ?, avatar = ? WHERE id = ?'
            );
            $stmt->execute([$firstName, $lastName, $email, $phoneNumber, $riderLevel, $avatarPath, $userId]);
            $messages[] = 'Profile updated successfully!';
            // Refresh user data after successful update
            $stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
            $stmt->execute([$userId]);
            $user = $stmt->fetch();

        } catch (PDOException $e) {
            error_log("Profile Update Error: " . $e->getMessage());
            $errors['db'] = 'Error updating profile. Please try again.';
        }
    }
}

// On a failed POST, user data should come from POST, otherwise from DB
$displayData = [
    'first_name' => $_POST['first_name'] ?? $user['first_name'],
    'last_name' => $_POST['last_name'] ?? $user['last_name'],
    'email' => $_POST['email'] ?? $user['email'],
    'phone_number' => $_POST['phone_number'] ?? $user['phone_number'],
    'rider_level' => $_POST['rider_level'] ?? $user['rider_level'],
    'avatar' => $user['avatar']
];

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Profile</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Lato:wght@300;400&display=swap" rel="stylesheet">
    <style>
        :root {
            --text-color: #ffffff;
            --text-color-secondary: #e0e0e0;
            --input-border-color: rgba(255, 255, 255, 0.8);
            --button-bg-color: #ffffff;
            --button-text-color: #000000;
            --error-color: #ffcdd2;
            --success-color: #c8e6c9;
            --error-text-color: #800020; /* A light red for text */
        }
        body {
            margin: 0;
            font-family: 'Lato', 'Hero Light', sans-serif;
            font-weight: 300;
            color: var(--text-color);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #000000 url('assets/pasted-20250910-070203-d672ef9e.jpg') no-repeat center center fixed;
            background-size: cover;
            text-align: center;
        }
        main {
            padding: 2rem; /* Reduced padding */
            width: auto;
        }
        .card {
            background: transparent;
            border: none;
            padding: 0;
            width: 100%;
            max-width: 800px; /* Wider for more fields */
            text-align: center;
        }
        .form-container-blur {
            background: rgba(255, 255, 255, 0.1); /* Lighter, more transparent background */
            -webkit-backdrop-filter: blur(12px); /* More blur */
            backdrop-filter: blur(12px);
            border-radius: 16px;
            padding: 2rem;
            border: 1px solid rgba(255, 255, 255, 0.18);
            box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1); /* Subtle shadow for depth */
            margin-bottom: 1.5rem; /* Space before the button */
        }
        h1 {
            font-size: 2.2rem;
            font-weight: 300;
            margin: 0 0 1.5rem; /* Reduced margin */
            letter-spacing: 1px;
            color: var(--text-color); /* Explicitly white */
        }
        form {
            display: flex;
            flex-direction: column;
            gap: 1rem; /* Reduced gap */
        }
        .form-row {
            display: flex;
            flex-wrap: wrap;
            gap: 1.5rem;
            align-items: flex-start; /* Align to top for error messages */
            justify-content: center;
        }
        .form-group {
            margin-bottom: 0;
            text-align: center;
            flex-grow: 1;
            flex-basis: 200px;
            position: relative; /* For error message positioning */
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-size: 0.9rem;
            color: var(--text-color-secondary); /* Improved contrast */
            text-align: left;
        }
        label[for="avatar"],
        label[for="first_name"],
        label[for="last_name"] {
            color: var(--text-color);
        }
        input[type="text"], input[type="email"], input[type="file"], input[type="tel"] {
            width: 100%;
            padding: 0.5rem 0; /* Reduced padding */
            background: transparent;
            border: none;
            border-bottom: 1px solid var(--input-border-color); /* Improved contrast */
            color: var(--text-color-secondary); /* Improved contrast */
            font-size: 1.1rem;
            box-sizing: border-box;
            font-family: 'Lato', sans-serif;
            font-weight: 300;
        }
        input:focus {
            outline: none;
            border-bottom-color: #ffffff;
        }
        .error-message {
            color: var(--error-text-color);
            font-size: 0.8rem;
            text-align: left;
            padding-top: 4px;
        }
        button {
            padding: 0.9rem 2rem;
            background: var(--button-bg-color);
            color: var(--button-text-color);
            border: none;
            border-radius: 25px;
            font-size: 1rem;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 1px;
            cursor: pointer;
            transition: background-color 0.2s ease, color 0.2s ease;
            align-self: center;
            margin-top: 1rem; /* Kept margin for button separation */
        }
        button:hover {
            background-color: #f0f0f0;
        }
        .avatar-preview {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            object-fit: cover;
            margin: 0 auto 1rem;
            border: 2px solid var(--input-border-color); /* Improved contrast */
            display: block;
        }
        .messages {
            padding: 1rem;
            margin-bottom: 1rem;
            border-radius: 8px;
            text-align: left;
            position: fixed;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 100;
            min-width: 300px;
            font-size: 0.9rem;
            background: rgba(0,0,0,0.7);
        }
        .errors { color: var(--error-color); border: 1px solid #d32f2f; }
        .success { color: var(--success-color); border: 1px solid #388e3c; }
        .back-link {
            color: var(--text-color);
            text-decoration: none;
            position: absolute;
            top: 1.5rem;
            right: 1.5rem;
            opacity: 0.8;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <a href="horses.php" class="back-link">Back to Horses</a>
    <main>
        <div class="card">
            <h1>Edit Profile</h1>
                        <form action="profile.php" method="post" enctype="multipart/form-data">
                <div class="form-container-blur">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="avatar">Avatar</label>
                            <?php if (!empty($displayData['avatar'])):
 ?>
                                <img src="<?= htmlspecialchars($displayData['avatar']) ?>?t=<?= time() ?>" alt="Avatar" class="avatar-preview">
                            <?php endif; ?>
                            <input type="file" id="avatar" name="avatar">
                            <?php if (isset($errors['avatar'])): ?><div class="error-message"><?= $errors['avatar'] ?></div><?php endif; ?>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="first_name">First Name</label>
                            <input type="text" id="first_name" name="first_name" value="<?= htmlspecialchars($displayData['first_name'] ?? '') ?>">
                            <?php if (isset($errors['first_name'])): ?><div class="error-message"><?= $errors['first_name'] ?></div><?php endif; ?>
                        </div>
                        <div class="form-group">
                            <label for="last_name">Last Name</label>
                            <input type="text" id="last_name" name="last_name" value="<?= htmlspecialchars($displayData['last_name'] ?? '') ?>">
                            <?php if (isset($errors['last_name'])): ?><div class="error-message"><?= $errors['last_name'] ?></div><?php endif; ?>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" id="email" name="email" value="<?= htmlspecialchars($displayData['email'] ?? '') ?>">
                            <?php if (isset($errors['email'])): ?><div class="error-message"><?= $errors['email'] ?></div><?php endif; ?>
                        </div>
                        <div class="form-group">
                            <label for="phone_number">Phone Number</label>
                            <input type="tel" id="phone_number" name="phone_number" value="<?= htmlspecialchars($displayData['phone_number'] ?? '') ?>" oninput="this.value=this.value.replace(/[^0-9]/g,'');">
                            <?php if (isset($errors['phone_number'])): ?><div class="error-message"><?= $errors['phone_number'] ?></div><?php endif; ?>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label for="rider_level">Rider Level</label>                        <input type="text" id="rider_level" name="rider_level" value="<?= htmlspecialchars($displayData['rider_level'] ?? '') ?>">
                        </div>
                    </div>
                </div>

                <button type="submit">Save Changes</button>
            </form>
        </div>
    </main>

    <?php if (!empty($messages) || isset($errors['db'])): ?>
    <div class="messages <?= isset($errors['db']) ? 'errors' : 'success' ?>">
        <?php if (isset($errors['db'])): ?>
            <p style="margin: 0;"><?= htmlspecialchars($errors['db']) ?></p>
        <?php endif; ?>
        <?php foreach ($messages as $message): ?>
            <p style="margin: 0;"><?= htmlspecialchars($message) ?></p>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>

</body>
</html>