<?php

namespace App\Console\Commands;

use App\Services\AppInstallerService;
use Illuminate\Console\Command;

class AppInstallCommand extends Command
{
    protected $signature = 'app:install
                            {app_id : 应用ID}';

    protected $description = '从本地目录安装应用';

    public function handle(AppInstallerService $installer): int
    {
        $appId = $this->argument('app_id');

        $this->info("正在从本地安装应用：{$appId}");

        try {
            $result = $installer->installFromLocal($appId);
        } catch (\Throwable $e) {
            $this->error("安装失败：{$e->getMessage()}");

            return self::FAILURE;
        }

        if (($result['code'] ?? -1) === 0) {
            $this->info($result['message'] ?? '安装成功');

            return self::SUCCESS;
        }

        $this->error($result['message'] ?? '安装失败');

        return self::FAILURE;
    }
}
