<?php
declare (strict_types = 1);

namespace app\command\OpenApiMgr;

use oauth\OAuthStorage;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class AddClient extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('OpenApiMgr:AddClient')
            ->addArgument('salt', Argument::OPTIONAL, "开放API的client_secret密钥的盐值")
            ->setDescription('开放API的client管理');
    }

    protected function execute(Input $input, Output $output)
    {
        $salt = $input->getArgument('salt');
        $salt = empty($salt) ? null : trim($salt);

        // 指令输出
        $oauth = new OAuthStorage($salt);

        $client_id = random_str(13, 'all', 0);
        $client_secret = random_str(32, 'all', 0);
        
        $ok = $oauth->addClient($client_id, $client_secret, null);
        if (!$ok) { 
            $output->writeln("添加失败");
            return;
        }

        $output->writeln("添加成功:\nClientID: {$client_id}\nClientSecret: {$client_secret}\n");
    }
}
