<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
ob_start(static function (string $html): string {
    $html = str_replace('<meta charset="utf-8">', '<head><meta charset="utf-8">', $html);
    $html = str_replace('</style><main>', '</style></head><body><main>', $html);
    return str_replace('</main></html>', '</main></body></html>', $html);
});
$user=require_login();$copy=['de'=>['Konto löschen','Private Notizen und Checklisten werden endgültig gelöscht. Kommentare bleiben anonymisiert erhalten.','Konto endgültig löschen','← Zum Profil','Der Hauptadministrator kann sein Konto nicht selbst löschen.','Dein Konto besitzt noch Reisen oder Katalogeinträge. Übertrage oder lösche diese zuerst.'],'en'=>['Delete account','Private notes and checklists are deleted permanently. Comments remain anonymously.','Delete account permanently','← Back to profile','The main administrator cannot delete their own account.','Your account still owns trips or catalogue entries. Transfer or delete them first.'],'fr'=>['Supprimer le compte','Les notes privées et listes seront supprimées définitivement. Les commentaires restent anonymes.','Supprimer définitivement le compte','← Retour au profil','L’administrateur principal ne peut pas supprimer son compte.','Votre compte possède encore des voyages ou entrées de catalogue.'],'es'=>['Eliminar cuenta','Las notas privadas y listas se eliminarán definitivamente. Los comentarios permanecen anónimos.','Eliminar cuenta definitivamente','← Volver al perfil','El administrador principal no puede eliminar su cuenta.','Tu cuenta todavía tiene viajes o entradas de catálogo.'],'it'=>['Elimina account','Le note private e le checklist saranno eliminate definitivamente. I commenti restano anonimi.','Elimina definitivamente l’account','← Torna al profilo','L’amministratore principale non può eliminare il proprio account.','Il tuo account possiede ancora viaggi o voci di catalogo.'],'nl'=>['Account verwijderen','Je privénotities en checklists worden definitief verwijderd. Reacties blijven anoniem bewaard.','Account definitief verwijderen','← Terug naar profiel','De hoofdbeheerder kan het eigen account niet verwijderen.','Je account bezit nog reizen of catalogusitems. Draag ze eerst over of verwijder ze.']];$l=$copy[language()]??$copy['de'];$error=null;
if($_SERVER['REQUEST_METHOD']==='POST'){verify_csrf();try{if($user['role']==='main_admin'){throw new RuntimeException($l[4]);}foreach(['trips','attractions','accommodations','restaurants','custom_points'] as $tableName){$q=db()->prepare('SELECT 1 FROM '.table($tableName).' WHERE created_by=:id LIMIT 1');$q->execute([':id'=>$user['id']]);if($q->fetchColumn()){throw new RuntimeException($l[5]);}}$pdo=db();$pdo->beginTransaction();foreach(['trip_private_notes','trip_checklist_items','magic_login_tokens','login_tokens','email_change_tokens','trip_admins','trip_participants'] as $tableName){try{$pdo->prepare('DELETE FROM '.table($tableName).' WHERE user_id=:id')->execute([':id'=>$user['id']]);}catch(Throwable $ignore){}}$q=$pdo->prepare('UPDATE '.table('users').' SET first_name=\'Ehemaliges\',last_name=\'Mitglied\',email=:email,phone=NULL,password_hash=NULL,profile_image=NULL,status=\'inactive\',deleted_at=NOW() WHERE id=:id');$q->execute([':email'=>'deleted-'.(int)$user['id'].'-'.time().'@deleted.invalid',':id'=>$user['id']]);$pdo->commit();$_SESSION=[];session_destroy();header('Location: /login.php?account_deleted=1');exit;}catch(Throwable $e){if(isset($pdo)&&$pdo->inTransaction()){$pdo->rollBack();}$error=$e->getMessage();}}
?><!doctype html><html lang="<?=htmlspecialchars(language(),ENT_QUOTES,'UTF-8')?>"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?=htmlspecialchars($l[0],ENT_QUOTES,'UTF-8')?> · TripTale</title><style>body{font-family:system-ui;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:600px;margin:auto;background:#fff;padding:28px;border-radius:14px}.button{display:inline-block;padding:10px 13px;border-radius:7px;background:#e3ebee;color:#126e82;text-decoration:none;font-weight:700;border:0}.delete{display:block;margin-top:20px;background:#9d2d2d;color:#fff}.error{padding:12px;background:#ffe6e3;color:#8b2424;border-radius:7px}@media(max-width:600px){body{padding:12px}main{padding:18px}}</style><main><a class="button" href="/profile.php"><?=htmlspecialchars($l[3],ENT_QUOTES,'UTF-8')?></a><h1><?=htmlspecialchars($l[0],ENT_QUOTES,'UTF-8')?></h1><p><?=htmlspecialchars($l[1],ENT_QUOTES,'UTF-8')?></p><?php if($error):?><p class="error"><?=htmlspecialchars($error,ENT_QUOTES,'UTF-8')?></p><?php endif;?><form method="post" onsubmit="return confirm(<?=json_encode($l[2],JSON_UNESCAPED_UNICODE)?>)"><input type="hidden" name="csrf_token" value="<?=htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8')?>"><button class="button delete"><?=htmlspecialchars($l[2],ENT_QUOTES,'UTF-8')?></button></form></main></html>
