<?php
// File: API_KONEKSI/customer/update_profil.php
header('Content-Type: application/json');
include '../config/koneksi.php';
include '../config/middleware.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $payload = authenticateJWT();
    $user_id = $payload['user_id'] ?? '';
    $nama = $_POST['nama'] ?? '';
    $email = $_POST['email'] ?? '';
    $wa = $_POST['wa'] ?? '';

    if (empty($user_id) || empty($nama) || empty($email) || empty($wa)) {
        echo json_encode(["status" => "gagal", "pesan" => "Data tidak boleh ada yang kosong!", "data" => []]);
        exit;
    }

    // Update tabel mst_user menggunakan Prepared Statement
    $query = "UPDATE mst_user SET lengkap_nm = ?, email_tx = ?, whatsapp_no = ? WHERE user_id = ?";
    $stmt = $conn->prepare($query);
    
    if ($stmt) {
        $stmt->bind_param("ssss", $nama, $email, $wa, $user_id);
        if ($stmt->execute()) {
            echo json_encode(["status" => "sukses", "pesan" => "Profil berhasil diperbarui!", "data" => []]);
        } else {
            echo json_encode(["status" => "gagal", "pesan" => "Gagal memperbarui profil di database", "data" => []]);
        }
        $stmt->close();
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Kesalahan sistem backend", "data" => []]);
    }
}
?>
