<?php
declare(strict_types=1);

namespace App\Console;

use App\Lib\ExpoPush;

final class PushSend
{
    public function run(array $argv = []): int
    {
        $opts = [];
        foreach ($argv as $a) {
            if (str_starts_with($a, '--') && str_contains($a, '=')) {
                [$k, $v] = explode('=', substr($a, 2), 2);
                $opts[$k] = $v;
            }
        }
        $token = $opts['token'] ?? '';
        $title = $opts['title'] ?? 'orchview2';
        $body  = $opts['body']  ?? '(no message)';
        if ($token === '') {
            fwrite(STDERR, "Missing --token=…\n");
            return 2;
        }
        $r = ExpoPush::sendRaw($token, $title, $body);
        fwrite(STDOUT, json_encode($r, JSON_PRETTY_PRINT) . "\n");
        return 0;
    }
}
