<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\Route;
use App\Model\RouteGroup;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Psr\Container\ContainerInterface;

#[Command]
class RouteGroupSeedCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('route-group:seed');
    }

    public function configure(): void
    {
        parent::configure();
        $this->setDescription('创建默认路由组并按前缀自动分配路由');
    }

    public function handle(): void
    {
        $default_groups = [
            ['name' => 'store-management',    'label' => '店铺管理',   'sort_order' => 1,  'patterns' => ['/api/v1/stores']],
            ['name' => 'company-management',  'label' => '公司管理',   'sort_order' => 2,  'patterns' => ['/api/v1/companies']],
            ['name' => 'product-management',  'label' => '产品管理',   'sort_order' => 3,  'patterns' => ['/api/v1/products']],
            ['name' => 'order-management',    'label' => '订单管理',   'sort_order' => 4,  'patterns' => ['/api/v1/orders', '/api/v1/order-items']],
            ['name' => 'refund-management',   'label' => '退款管理',   'sort_order' => 5,  'patterns' => ['/api/v1/refunds', '/api/v1/refund-items']],
            ['name' => 'raw-data',            'label' => '原始数据',   'sort_order' => 6,  'patterns' => ['/api/v1/raw/']],
            ['name' => 'log-management',      'label' => '日志管理',   'sort_order' => 7,  'patterns' => ['/api/v1/logs/']],
            ['name' => 'user-permission',     'label' => '用户与权限', 'sort_order' => 8,  'patterns' => ['/api/v1/users', '/api/v1/roles', '/api/v1/route-groups', '/api/v1/routes']],
            ['name' => 'platform-management', 'label' => '平台管理',   'sort_order' => 9,  'patterns' => ['/api/v1/platforms']],
            ['name' => 'system',              'label' => '系统功能',   'sort_order' => 10, 'patterns' => ['/api/v1/me/', '/api/v1/dashboard', '/api/v1/mq', '/api/v1/failed-messages', '/api/v1/auth/']],
            ['name' => 'materialization-admin', 'label' => '物化任务管理', 'sort_order' => 11, 'patterns' => ['/api/v1/admin/materialization/']],
        ];

        $group_count = 0;
        $route_count = 0;

        foreach ($default_groups as $config) {
            $patterns = $config['patterns'];
            unset($config['patterns']);

            $group = RouteGroup::query()->updateOrCreate(
                ['name' => $config['name']],
                ['label' => $config['label'], 'sort_order' => $config['sort_order']]
            );
            $group_count++;

            // 按前缀匹配路由并分配到组
            foreach ($patterns as $pattern) {
                $affected = Route::query()
                    ->where('path', 'like', $pattern . '%')
                    ->update(['group_id' => $group->id]);
                $route_count += $affected;
            }
        }

        $this->info("Seeded {$group_count} route groups, assigned {$route_count} routes.");

        $unassigned = Route::query()->whereNull('group_id')->count();
        if ($unassigned > 0) {
            $this->warn("Warning: {$unassigned} routes remain unassigned.");
        }
    }
}
