<?php

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Support\Guard;

$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();
Guard::requireAbilityOrForbidden(\MintyPHP\Module\AddressBook\AddressBookAuthorizationPolicy::ABILITY_VIEW);

$currentUserId = (int) ($session['user']['id'] ?? 0);
$uuid = trim((string) ($id ?? ''));
if ($uuid === '') {
    http_response_code(400);
    return;
}

$addressBookService = app(\MintyPHP\Module\AddressBook\Service\AddressBookService::class);
$viewContext = $addressBookService->buildViewContext($currentUserId, $uuid);
$status = (string) ($viewContext['status'] ?? '');
if ($status === 'not_found') {
    http_response_code(404);
    $user = null;
    return;
}
if ($status === 'forbidden') {
    http_response_code(403);
    $user = null;
    return;
}
$user = $viewContext['user'] ?? null;
if (!is_array($user)) {
    http_response_code(404);
    $user = null;
    return;
}
