<?php
require_once '../core/db.php';
require_once '../core/response.php';

$data = json_decode(file_get_contents('php://input'),true);
$row = fetchOne("SELECT * FROM email_logs WHERE token=:t",['t'=>$data['token']]);

if(!$row || $row['is_used'] || strtotime($row['expired_at'])<time()){
jsonResponse(['success'=>false,'error'=>'Link tidak valid']);
}

if(strlen($data['password'])<6 || $data['password']!==$data['confirm']){
jsonResponse(['success'=>false,'error'=>'Password tidak valid']);
}

execute("UPDATE player SET password_hash=:p WHERE email=:e",[
'p'=>password_hash($data['password'],PASSWORD_BCRYPT),
'e'=>$row['email']
]);

execute("UPDATE email_logs SET is_used=1 WHERE id=:id",['id'=>$row['id']]);
jsonResponse(['success'=>true]);
