<?php
/**
 * @var string $assetsUrl
 * @var string $baseUrl
 * @var string $mode
 * @var array $targetUser
 * @var string|null $error
 */
$__isEdit = $mode === 'edit';
$__uname  = (string) ($targetUser['username'] ?? '');
$__parts  = preg_split('/[^A-Za-z0-9]+/', $__uname, -1, PREG_SPLIT_NO_EMPTY) ?: [];
$__initials = $__isEdit && $__uname !== ''
    ? strtoupper(substr($__parts[0] ?? $__uname, 0, 1) . (isset($__parts[1]) ? substr($__parts[1], 0, 1) : substr($__uname, 1, 1)))
    : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $__isEdit ? 'Edit user' : 'Create user' ?> · <?= htmlspecialchars(\PpPhp\Core\Brand::name()) ?></title>
    <?php include constant('BASE_PATH') . '/core/views/favicon.php'; ?>
    <link href="<?= $assetsUrl ?>/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?= $assetsUrl ?>/icons/bootstrap-icons.css" rel="stylesheet">
    <?php include constant('BASE_PATH') . '/core/views/theme-head.php'; ?>
    <link href="<?= $assetsUrl ?>/css/account.css?v=<?= (int) @filemtime(constant('BASE_PATH') . '/public/assets/css/account.css') ?>" rel="stylesheet">
</head>
<body class="acct-body">
    <header class="acct-top">
        <a class="acct-brand-min" href="<?= rtrim($baseUrl, '/') ?>/projects" aria-label="Home">
            <?= \PpPhp\Core\Brand::lockup(30) ?>
        </a>
        <div class="acct-top-actions">
            <a href="<?= $baseUrl ?>/users" class="acct-back"><i class="bi bi-arrow-left"></i> Users</a>
            <?php include constant('BASE_PATH') . '/core/views/theme-toggle.php'; ?>
        </div>
    </header>

    <main class="acct-main">
        <div class="acct-card">
            <div class="acct-head">
                <div class="acct-avatar<?= $__isEdit ? '' : ' is-key' ?>"><?= $__isEdit ? htmlspecialchars($__initials ?: 'U') : '<i class="bi bi-person-plus"></i>' ?></div>
                <div class="acct-head-copy">
                    <div class="acct-eyebrow">Users</div>
                    <h1 class="acct-title"><?= $__isEdit ? 'Edit user' : 'Create user' ?></h1>
                    <p class="acct-sub"><?= $__isEdit ? 'Update this account’s details, role and password.' : 'Add a new account and choose what it can do.' ?></p>
                </div>
            </div>

            <?php if (!empty($error)): ?>
                <div class="acct-alert is-error"><i class="bi bi-exclamation-triangle-fill"></i><span><?= htmlspecialchars($error) ?></span></div>
            <?php endif; ?>
            <?php if (!empty($requestContext)): ?>
                <div class="acct-alert is-info"><i class="bi bi-lightning-charge"></i><span>
                    <strong>Approving a trial request.</strong>
                    <?= htmlspecialchars((string) $requestContext['full_name']) ?> asked for
                    <?= htmlspecialchars(\PpPhp\Core\Auth\TemporaryAccessRequestService::durationLabel((int) $requestContext['requested_duration_hours'])) ?>.
                    Their email is filled in below — pick a username and password to finish.
                </span></div>
            <?php endif; ?>

            <form method="post" action="<?= $__isEdit ? $baseUrl . '/users/edit/' . $targetUser['id'] : $baseUrl . '/users/create' ?>">
                <?= \PpPhp\Core\Security::CSRF()->HiddenField() ?>
                <?php if (!empty($requestContext)): ?>
                    <input type="hidden" name="from_request" value="<?= (int) $requestContext['id'] ?>">
                <?php endif; ?>
                <div class="acct-field">
                    <label class="acct-label" for="uf-username">Username</label>
                    <input id="uf-username" type="text" name="username" class="acct-input" value="<?= htmlspecialchars($__uname) ?>" <?= $__isEdit ? 'readonly' : 'required' ?>>
                    <?php if ($__isEdit): ?><div class="acct-hint"><i class="bi bi-lock-fill"></i> Usernames can’t be changed.</div><?php endif; ?>
                </div>
                <div class="acct-field">
                    <label class="acct-label" for="uf-email">Email address</label>
                    <input id="uf-email" type="email" name="email" class="acct-input" value="<?= htmlspecialchars($targetUser['email'] ?? '') ?>" placeholder="name@example.com" required>
                </div>
                <div class="acct-field">
                    <label class="acct-label" for="uf-password">Password <?php if ($__isEdit): ?><span style="text-transform:none;font-weight:500;opacity:.7">(leave blank to keep)</span><?php endif; ?></label>
                    <input id="uf-password" type="password" name="password" class="acct-input" autocomplete="new-password" <?= $__isEdit ? '' : 'required' ?>>
                </div>
                <div class="acct-field">
                    <label class="acct-label" for="uf-role">Role</label>
                    <select id="uf-role" name="role" class="acct-input">
                        <option value="developer" <?= ($targetUser['role'] ?? '') === 'developer' ? 'selected' : '' ?>>Developer</option>
                        <option value="user" <?= ($targetUser['role'] ?? '') === 'user' ? 'selected' : '' ?>>User</option>
                        <?php if (\PpPhp\Core\Security::IsAdmin()): ?>
                            <option value="superadmin" <?= ($targetUser['role'] ?? '') === 'superadmin' ? 'selected' : '' ?>>Superadmin</option>
                        <?php endif; ?>
                    </select>
                    <?php if (!\PpPhp\Core\Security::IsAdmin()): ?>
                        <div class="acct-hint"><i class="bi bi-info-circle"></i> Only an administrator can grant the Superadmin role.</div>
                    <?php endif; ?>
                </div>
                <div class="acct-actions">
                    <button type="submit" class="acct-btn acct-btn-primary"><i class="bi bi-check-lg"></i> <?= $__isEdit ? 'Update user' : 'Create user' ?></button>
                    <a href="<?= $baseUrl ?>/users" class="acct-btn acct-btn-ghost"><i class="bi bi-x-lg"></i> Cancel</a>
                </div>
            </form>
        </div>
    </main>

    <footer class="acct-foot"><?= htmlspecialchars(\PpPhp\Core\Brand::name()) ?><?php $__tg = \PpPhp\Core\Brand::tagline(); if ($__tg !== ''): ?> · <?= htmlspecialchars($__tg) ?><?php endif; ?></footer>
    <script src="<?= $assetsUrl ?>/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
