<?php

namespace Kylin\Licensing\Commands;

use Illuminate\Console\Command;
use Kylin\Licensing\Services\LicenseService;
use Kylin\Licensing\Services\LicenseVerifier;

/**
 * 许可证激活命令
 */
class LicenseActivateCommand extends Command
{
    protected $signature = 'kylin:license-activate {key : 许可证密钥} {--domain= : 绑定域名} {--ip= : 绑定IP}';

    protected $description = '激活 Kylin 许可证（远程校验）';

    public function handle(LicenseService $licenseService, LicenseVerifier $verifier): int
    {
        $key = $this->argument('key');
        $domain = $this->option('domain');
        $ip = $this->option('ip');

        $this->info('正在激活许可证...');

        try {
            $data = $licenseService->activate($key, $domain, $ip);
            $this->components->info('许可证激活成功！');
            $this->components->twoColumnDetail('密钥', $data['license_key'] ?? $key);
            $this->components->twoColumnDetail('类型', $data['type'] ?? '-');
            $this->components->twoColumnDetail('过期时间', $data['expires_at'] ?? '永久');
            $this->newLine();
            $this->components->info("请将以下值添加到 .env 文件：");
            $this->line("KYLIN_LICENSE_KEY={$key}");
            return self::SUCCESS;
        } catch (\Throwable $e) {
            $this->components->error('许可证激活失败：' . $e->getMessage());
            return self::FAILURE;
        }
    }
}
