<?php
declare(strict_types=1);
require dirname(__DIR__).'/app/Services/SubscriptionPolicy.php';
require dirname(__DIR__).'/app/Services/TableAccountAccess.php';

use App\Services\SubscriptionPolicy;
use App\Services\TableAccountAccess;

$checks=0;
function ok(string $label, bool $pass):void {
    global $checks;
    if (!$pass) throw new RuntimeException('FAIL: '.$label);
    $checks++;
    echo 'OK '.$label."\n";
}

// Fail-closed Beeper entitlement cases, including the old broken fallback to ["*"].
foreach ([false, null, '', '{broken', '{}', '[]', '["pos"]', '["virtual_beeper", 7]'] as $raw) {
    ok('Beeper no authorized by invalid/absent/irrelevant plan '.var_export($raw,true),
        !SubscriptionPolicy::allows(SubscriptionPolicy::fromJson($raw),'virtual_beeper'));
}
ok('Explicit Beeper permission is allowed', SubscriptionPolicy::allows(SubscriptionPolicy::fromJson('["virtual_beeper"]'),'virtual_beeper'));
ok('Explicit wildcard allowed with valid active plan',SubscriptionPolicy::allows(SubscriptionPolicy::fromJson('["*"]'),'virtual_beeper'));

// 32 random bytes seeded for reproducible pure-function tests; not a real key.
$key = str_repeat('K', 32);
$t1 = TableAccountAccess::signWithKey(11, 2, 101, $key);
ok('Token exactly 64 lowercase hex', preg_match('/^[0-9a-f]{64}$/D', $t1)===1);
ok('Valid token grants the specific table', TableAccountAccess::validateWithKey(11,2,101,$t1,$key));
ok('Different table denied', !TableAccountAccess::validateWithKey(11,2,102,$t1,$key));
ok('Different branch denied', !TableAccountAccess::validateWithKey(11,3,101,$t1,$key));
ok('Different restaurant denied', !TableAccountAccess::validateWithKey(12,2,101,$t1,$key));
ok('Wrong key denied', !TableAccountAccess::validateWithKey(11,2,101,$t1,str_repeat('Q',32)));
ok('Missing token denied', !TableAccountAccess::validateWithKey(11,2,101,'',$key));
ok('Malformed token denied', !TableAccountAccess::validateWithKey(11,2,101,str_repeat('x',64),$key));
ok('Short secret cannot authorize', !TableAccountAccess::validateWithKey(11,2,101,$t1,'short'));

$beeper=file_get_contents(dirname(__DIR__).'/app/Services/VirtualBeeperService.php');
ok('Beeper public endpoint uses strict policy',str_contains($beeper,'SubscriptionPolicy::fromJson($plan->fetchColumn())'));
ok('Beeper public endpoint does not grant wildcard by default',!str_contains($beeper,"?:'[\"*\"]'"));
$public=file_get_contents(dirname(__DIR__).'/api/public.php');
ok('Account endpoint checks signed authorization',str_contains($public,'TableAccountAccess::allows($rid, (int)$branch[\'id\'], $tableId, $access)'));
$menu=file_get_contents(dirname(__DIR__).'/menu.php');
ok('Menu does not publicly mint tokens',!str_contains($menu,'TableAccountAccess::issue('));
ok('No Referer from protected menu',str_contains($menu,"header('Referrer-Policy: no-referrer')"));
$admin=file_get_contents(dirname(__DIR__).'/api/index.php');
ok('Admin QR generator embeds signed token',str_contains($admin,'TableAccountAccess::issue($rid,$bid,(int)$publicTable[\'id\'])'));
$client=file_get_contents(dirname(__DIR__).'/public/assets/menu.js');
ok('Account fetch requires token in client',str_contains($client,'!tableAccountAccess'));
echo "PASARON {$checks} COMPROBACIONES W08.1\n";
