<?php
/**
 * forgotpassword_api.php
 * User-এর Password-Reset Request Save করে (Admin পরে Approve করবে)
 */
include __DIR__ . "/deepanshu/conn.php";
header('Content-Type: application/json; charset=utf-8');

$input = json_decode(file_get_contents('php://input'), true);
$mobile = trim($input['mobile'] ?? '');
$newPassword = $input['newPassword'] ?? '';
$verifyNote = trim($input['verifyNote'] ?? '');

if (!preg_match('/^01[0-9]{9}$/', $mobile) || strlen($newPassword) < 6 || empty($verifyNote)) {
    echo json_encode(['code' => 1, 'msg' => 'সঠিক তথ্য দাও']);
    exit;
}

// ✅ নিশ্চিত করা, এই Mobile Number দিয়ে আসলেই Account আছে কিনা
$check = $conn->prepare("SELECT id FROM shonu_subjects WHERE mobile = ?");
$check->bind_param("s", $mobile);
$check->execute();
if ($check->get_result()->num_rows === 0) {
    echo json_encode(['code' => 1, 'msg' => 'এই Mobile Number-এ কোনো Account পাওয়া যায়নি']);
    exit;
}

$stmt = $conn->prepare("INSERT INTO password_reset_requests (mobile, new_password, verification_note) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $mobile, $newPassword, $verifyNote);
$stmt->execute();

echo json_encode(['code' => 0, 'msg' => 'Request জমা হয়েছে']);
