<?php

use MintyPHP\Http\ApiAuth;
use MintyPHP\Http\ApiBootstrap;
use MintyPHP\Http\ApiResponse;

ApiBootstrap::init();
ApiResponse::requireMethod('POST');
$apiTokenService = app(\MintyPHP\Service\Auth\ApiTokenService::class);
$apiTokenEndpointService = app(\MintyPHP\Service\Auth\ApiTokenEndpointService::class);

$userId = ApiAuth::userId();
if ($userId <= 0) {
    ApiResponse::unauthorized();
}

ApiAuth::requireSelfManageTokens();

$scopedTenantId = ApiAuth::scopedTenantId();
$scopedTenantUuid = $apiTokenEndpointService->resolveTenantUuidById($scopedTenantId);
$result = $apiTokenService->revokeAllForUser($userId, $scopedTenantId);

ApiResponse::success([
    'data' => [
        'revoked_count' => (int) ($result['count'] ?? 0),
        'tenant_uuid' => $scopedTenantUuid,
    ],
]);
