<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', 'E-Mail-Adresse bestätigen · TripTale' => t('confirm_email.title') . ' · TripTale', '>E-Mail-Adresse bestätigen<' => '>' . t('confirm_email.title') . '<', '>Deine neue E-Mail-Adresse wurde bestätigt und gespeichert.<' => '>' . t('confirm_email.success') . '<', '>Zum Profil<' => '>' . t('confirm_email.back') . '<']);
});
$id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT,['options'=>['min_range'=>1]]);$token=(string)($_GET['token']??'');$error=null;$success=false;
if(!$id||!preg_match('/^[a-f0-9]{64}$/',$token)){$error=t('confirm_email.error_invalid');}
else{try{$pdo=db();$pdo->beginTransaction();$query=$pdo->prepare('SELECT ect.id,ect.user_id,ect.new_email,ect.token_hash FROM '.table('email_change_tokens').' ect JOIN '.table('users').' u ON u.id=ect.user_id WHERE ect.id=:id AND ect.used_at IS NULL AND ect.expires_at>NOW() AND u.status=\'active\' AND u.deleted_at IS NULL FOR UPDATE');$query->execute([':id'=>$id]);$record=$query->fetch();if(!$record||!hash_equals((string)$record['token_hash'],hash('sha256',$token))){$pdo->rollBack();$error=t('confirm_email.error_invalid');}else{$exists=$pdo->prepare('SELECT id FROM '.table('users').' WHERE email=:email AND id<>:id LIMIT 1');$exists->execute([':email'=>$record['new_email'],':id'=>$record['user_id']]);if($exists->fetchColumn()){$pdo->rollBack();$error=t('confirm_email.error_used');}else{$pdo->prepare('UPDATE '.table('users').' SET email=:email WHERE id=:id')->execute([':email'=>$record['new_email'],':id'=>$record['user_id']]);$pdo->prepare('UPDATE '.table('email_change_tokens').' SET used_at=NOW() WHERE id=:id')->execute([':id'=>$record['id']]);$pdo->commit();$success=true;}}}catch(Throwable $exception){if(isset($pdo)&&$pdo->inTransaction()){$pdo->rollBack();}$error=t('confirm_email.error_confirm');}}
?><!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>E-Mail-Adresse bestätigen · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:560px;margin:auto;background:#fff;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}.button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border-radius:7px;text-decoration:none;font-weight:700}.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}.button{display:block;text-align:center}}</style></head><body><main><h1>E-Mail-Adresse bestätigen</h1><?php if($success):?><p class="success">Deine neue E-Mail-Adresse wurde bestätigt und gespeichert.</p><a class="button" href="/profile.php">Zum Profil</a><?php else:?><p class="error"><?=htmlspecialchars((string)$error,ENT_QUOTES,'UTF-8')?></p><a class="button" href="/profile.php">Zum Profil</a><?php endif;?></main></body></html>
