<?php
session_start();
include '../../connection.php';

if (isset($_POST['updating_informations'])) {
    $first_name       = $_POST['first_name'] ?? "";
    $middle_name      = $_POST['middle_name'] ?? "";
    $last_name        = $_POST['last_name'] ?? "";
    $email            = $_POST['email'] ?? "";
    $contact          = $_POST['contact'] ?? "";
    $address          = $_POST['address'] ?? "";
    $age              = $_POST['age'] ?? "";
    $birthday         = $_POST['birthday'] ?? "";
    $gender           = $_POST['gender'] ?? "";
    $status           = $_POST['status'] ?? "";
    $date_created     = $_POST['date_created'] ?? "";
    $current_password = $_POST['current_password'] ?? "";
    $new_password     = $_POST['new_password'] ?? "";
    $confirm_password = $_POST['confirm_password'] ?? "";
    $resident_id      = $_SESSION['resident_id'];

    $response = [];

    // ✅ 1. Update basic profile info
    $update_query = "UPDATE `tbl_resident` SET 
        `first_name`='$first_name',
        `middle_name`='$middle_name',
        `last_name`='$last_name',
        `email`='$email',
        `address`='$address',
        `contact`='$contact',
        `age`='$age',
        `birthday`='$birthday',
        `gender`='$gender'
        WHERE `resident_id`='$resident_id'";

    if ($conn->query($update_query)) {
        $response[] = "Profile updated successfully";
    } else {
        $response[] = "Failed to update profile: " . $conn->error;
    }

    // ✅ 2. Handle password update if fields are filled
    if (!empty($current_password) || !empty($new_password) || !empty($confirm_password)) {

        // 🔐 Step 1: Check current password
        $check_password = "SELECT password FROM tbl_resident WHERE resident_id = '$resident_id' AND password = '$current_password'";
        $result = $conn->query($check_password);

        if ($result && $result->num_rows > 0) {
            // ✅ Step 2: Check if new password matches confirmation
            if ($new_password !== $confirm_password) {
                $response[] = "New password and confirmation do not match!";
            } else {
                // ✅ Step 3: Update password
                $update_password = "UPDATE `tbl_resident` SET `password`='$new_password' WHERE `resident_id`='$resident_id'";
                if ($conn->query($update_password)) {
                    $response[] = "Password updated successfully!";
                } else {
                    $response[] = "Failed to update password: " . $conn->error;
                }
            }
        } else {
            $response[] = "Current password is incorrect!";
        }
    }

    echo implode("\n", $response);
}

if (isset($_POST['fetching_info'])) {
    header("Content-Type: application/json");

    // assuming you store resident_id in session
    $resident_id = $_SESSION['resident_id']; 

    $query = "SELECT * FROM tbl_resident WHERE resident_id = '$resident_id'";
    $query_result = $conn->query($query);

    if ($query_result && $query_result->num_rows > 0) {
        $row = $query_result->fetch_assoc(); // fetch single record
        echo json_encode($row);
    } else {
        echo json_encode(["error" => "No record found"]);
    }
}
?>
