<?php
declare(strict_types=1);

require_once __DIR__ . '/app/Helpers/bootstrap.php';
$user = require_login();
if (($user['role'] ?? '') !== 'main_admin') { http_response_code(403); exit(t('main_admin_password.error_access')); }

$error = null;
$saved = isset($_GET['saved']);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    $password = (string)($_POST['password'] ?? '');
    $confirmation = (string)($_POST['password_confirmation'] ?? '');
    if (strlen($password) < 12) { $error = t('main_admin_password.error_length'); }
    elseif ($password !== $confirmation) { $error = t('main_admin_password.error_match'); }
    else {
        try {
            $query = db()->prepare('UPDATE ' . table('users') . ' SET password_hash=:hash WHERE id=:id AND role=\'main_admin\' AND status=\'active\' AND deleted_at IS NULL');
            $query->execute([':hash' => password_hash($password, PASSWORD_DEFAULT), ':id' => $user['id']]);
            if ($query->rowCount() !== 1) { throw new RuntimeException(); }
            header('Location: /main-admin-password.php?saved=1');
            exit;
        } catch (Throwable $exception) { $error = t('main_admin_password.error_save'); }
    }
}
?>
<!doctype html><html lang="<?= htmlspecialchars(language(), ENT_QUOTES, 'UTF-8') ?>"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?= htmlspecialchars(t('main_admin_password.title'), ENT_QUOTES, 'UTF-8') ?> · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:620px;margin:auto;background:#fff;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}.button,button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700;cursor:pointer}.back{background:#e3ebee;color:#126e82}label{display:block;margin-top:18px;font-weight:700}input{box-sizing:border-box;width:100%;padding:11px;margin-top:6px;border:1px solid #b9c1c8;border-radius:7px;font:inherit}.save{margin-top:22px}.hint{color:#59636d}.success,.error{padding:12px;border-radius:8px}.success{background:#e6f5e9;color:#1d6831}.error{background:#ffe6e3;color:#8b2424}@media(max-width:600px){body{padding:12px}main{padding:16px}.save{width:100%}}</style></head><body><main><a class="button back" href="/profile.php"><?= htmlspecialchars(t('main_admin_password.back'), ENT_QUOTES, 'UTF-8') ?></a><h1><?= htmlspecialchars(t('main_admin_password.title'), ENT_QUOTES, 'UTF-8') ?></h1><p class="hint"><?= htmlspecialchars(t('main_admin_password.intro'), ENT_QUOTES, 'UTF-8') ?></p><?php if ($saved): ?><p class="success"><?= htmlspecialchars(t('main_admin_password.saved'), ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?><?php if ($error): ?><p class="error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?><form method="post"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(), ENT_QUOTES, 'UTF-8') ?>"><label><?= htmlspecialchars(t('main_admin_password.password'), ENT_QUOTES, 'UTF-8') ?><input name="password" type="password" minlength="12" autocomplete="new-password" required></label><label><?= htmlspecialchars(t('main_admin_password.confirmation'), ENT_QUOTES, 'UTF-8') ?><input name="password_confirmation" type="password" minlength="12" autocomplete="new-password" required></label><button class="save" type="submit"><?= htmlspecialchars(t('main_admin_password.save'), ENT_QUOTES, 'UTF-8') ?></button></form></main></body></html>
