<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');
include '../../../../config/db.php';

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $userId = mysqli_real_escape_string($con, $_GET['userId']);
    $query = "SELECT * FROM Family WHERE userId = '$userId'";
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) > 0) {
        $families = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $families[] = $row;
        }
        echo json_encode($families);
        http_response_code(200);
    } else {
        echo json_encode([
            'status' => 0,
            'message' => 'No families found for the given user ID.',
        ]);
        http_response_code(200);
    }
} else {
    echo json_encode([
        'status' => 0,
    ]);
    die();
}
