<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Use correct config path
$config_path = dirname(__DIR__, 2) . '/include/config.php';
if (file_exists($config_path)) {
    include($config_path);
} else {
    echo json_encode(['status' => '0', 'msg' => 'Configuration file not found']);
    exit();
}

// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    echo json_encode(['status' => '0', 'msg' => 'Unauthorized access']);
    exit();
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $current_password = $_POST['current_password'];
    $new_password = $_POST['new_password'];
    $confirm_password = $_POST['confirm_password'];
    $user_id = $_SESSION['user_id'];
    
    // Validate inputs
    if (empty($current_password) || empty($new_password) || empty($confirm_password)) {
        echo json_encode(['status' => '0', 'msg' => 'All password fields are required']);
        exit();
    }
    
    // Check if new passwords match
    if ($new_password !== $confirm_password) {
        echo json_encode(['status' => '0', 'msg' => 'New passwords do not match']);
        exit();
    }
    
    // Validate password length
    if (strlen($new_password) < 6) {
        echo json_encode(['status' => '0', 'msg' => 'Password must be at least 6 characters long']);
        exit();
    }
    
    try {
        // Get current password hash from database
        $stmt = $pdo->prepare("SELECT password FROM users WHERE id = :user_id");
        $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
        $stmt->execute();
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$user) {
            echo json_encode(['status' => '0', 'msg' => 'User not found']);
            exit();
        }
        
        // Verify current password
        if (!password_verify($current_password, $user['password'])) {
            echo json_encode(['status' => '0', 'msg' => 'Current password is incorrect']);
            exit();
        }
        
        // Hash new password
        $new_password_hash = password_hash($new_password, PASSWORD_DEFAULT);
        
        // Update password
        $stmt = $pdo->prepare("UPDATE users SET password = :password, updated_at = NOW() WHERE id = :user_id");
        $stmt->bindParam(':password', $new_password_hash);
        $stmt->bindParam(':user_id', $user_id, PDO::PARAM_INT);
        
        if ($stmt->execute()) {
            echo json_encode([
                'status' => '1', 
                'msg' => 'Password changed successfully'
            ]);
        } else {
            echo json_encode(['status' => '0', 'msg' => 'Failed to change password']);
        }
        
    } catch (PDOException $e) {
        echo json_encode(['status' => '0', 'msg' => 'Database error: ' . $e->getMessage()]);
    }
} else {
    echo json_encode(['status' => '0', 'msg' => 'Invalid request method']);
}
?>