<?php
session_start();
header('Content-Type: application/json');
require_once "../includes/functions.php"; 
$user_id = $_SESSION['user_id'] ?? 0;

$oldPassword = trim($_POST['inputoldpassword'] ?? '');
$newPassword   = trim($_POST['inputnewpassword'] ?? '');

$confirmPassword = trim($_POST['inputconfirmpassword'] ?? '');

if (empty($oldPassword) || empty($newPassword) || empty($confirmPassword)) {
    echo json_encode(["status" => "error", "message" => "all field required"]);
    exit;
}


if(!$user_id){
    echo json_encode(["status"=>"error","message"=>"Unauthorized"]);
    exit;
}

if($confirmPassword != $newPassword){
   echo json_encode(["status"=>"error","message"=>"comfirm password does not match"]);
    exit; 
}

$response = changePassword($user_id, $oldPassword, $newPassword);
if ($response["status"] === "success") {
    echo json_encode($response);
} else {
    echo json_encode($response);
}
