<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE,REQUEST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

require 'db/config.php';
date_default_timezone_set("Africa/Lagos");

function check_input($par) {
    return htmlspecialchars(stripslashes(trim($par)));
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // List of required fields
    $required_fields = ['user_id', 'auth', 'fname', 'lname', 'phone', 'city', 'state', 'country', 'height', 'body_color', 'gender', 'profession'];
    $input_data = [];

    foreach ($required_fields as $field) {
        if (empty($_POST[$field])) {
            echo json_encode(['status' => false, 'success' => "$field is required"]);
            exit;
        }
        $input_data[$field] = mysqli_real_escape_string($con, check_input($_POST[$field]));
    }

    // Prepare the update statement
    $stmt = $con->prepare("UPDATE users SET 
        about = ?, 
        firstname = ?, 
        lastname = ?, 
        phone = ?, 
        city = ?, 
        state = ?, 
        country = ?, 
        height = ?, 
        body_color = ?, 
        gender = ?, 
        profession = ? 
        WHERE user_id = ? AND auth = ?");

    $stmt->bind_param("ssssssssssss",
        $input_data['about'],
        $input_data['fname'],
        $input_data['lname'],
        $input_data['phone'],
        $input_data['city'],
        $input_data['state'],
        $input_data['country'],
        $input_data['height'],
        $input_data['body_color'],
        $input_data['gender'],
        $input_data['profession'],
        $input_data['user_id'],
        $input_data['auth']
    );

    if ($stmt->execute()) {
        echo json_encode(['status' => true, 'success' => 'Profile updated successfully']);
    } else {
        echo json_encode(['status' => false, 'success' => 'Failed to update profile']);
    }

    $stmt->close();
    $con->close();
}
?>
