<?php

namespace Kylin\Rbac\Commands;

use Illuminate\Console\Command;

/**
 * RBAC 安装命令
 *
 * Artisan 命令：php artisan kylin:rbac-install
 * 安装 RBAC 初始数据（角色和权限种子数据）。
 */
class InstallCommand extends Command
{
    protected $signature = 'kylin:rbac-install';

    protected $description = '安装 Kylin RBAC 初始数据';

    public function handle(): int
    {
        $this->info('正在安装 Kylin RBAC 初始数据...');

        $rbacService = app(\Kylin\Rbac\Services\RbacService::class);

        // 创建默认角色
        $roles = [
            ['name' => 'super-admin', 'display_name' => '超级管理员', 'description' => '拥有所有权限'],
            ['name' => 'admin', 'display_name' => '管理员', 'description' => '系统管理员'],
            ['name' => 'editor', 'display_name' => '编辑', 'description' => '内容编辑'],
            ['name' => 'viewer', 'display_name' => '查看者', 'description' => '只读查看'],
        ];

        foreach ($roles as $role) {
            \Kylin\Rbac\Models\Role::firstOrCreate(
                ['name' => $role['name'], 'guard_name' => 'web'],
                ['display_name' => $role['display_name'], 'description' => $role['description']],
            );
            $this->components->info("角色 [{$role['display_name']}] 已创建");
        }

        $this->newLine();
        $this->components->info('Kylin RBAC 安装完成！');
        return self::SUCCESS;
    }
}
