<?php
declare(strict_types=1);
namespace app\common;

final class Serve
{
    public const CAPTURE_MODE='HABIT_OWN_STAFF_NO_QUEUE_V1';
    public static function pushJump(string $action,array $data=[]):bool{return self::capture('pushJump',$action,$data);}
    public static function push(string $action,array $data=[],int $delay=0):bool{return self::capture('push',$action,$data);}
    private static function capture(string $kind,string $action,array $data):bool
    {
        $runtime=(string)getenv('CATEGORY_RUNTIME');
        $map=json_decode((string)file_get_contents($runtime.'/private-staff-map.json'),true);
        $staff=$data['staffId']??null;$store=$data['storeId']??null;$date=$data['date']??null;
        $valid=$action==='staffPower'&&is_int($staff)&&is_int($store)
            &&isset($map[(string)$staff])&&$map[(string)$staff]===$store
            &&is_string($date)&&preg_match('/^\d{4}-\d{2}-\d{2}$/D',$date)===1;
        file_put_contents($runtime.'/serve-captures.jsonl',json_encode(['kind'=>$kind,'action'=>$action,
            'staff_id'=>$staff,'store_id'=>$store,'date'=>$date,'owned_parameters_valid'=>$valid,
            'real_queue_executed'=>false])."\n",FILE_APPEND|LOCK_EX);
        chmod($runtime.'/serve-captures.jsonl',0600);
        if(!$valid)throw new \RuntimeException('Private queue boundary denied non-owned payload');
        return true;
    }
}
final class UserApi
{
    public function run($type,$params=[]):array{throw new \RuntimeException('External UserApi is forbidden in Habit batch');}
}
