<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Category;
use Illuminate\Support\Facades\DB;

class FixCategoryHierarchySeeder extends Seeder
{
    public function run()
    {
        // 1. Reset all to root first
        Category::query()->update(['parent_id' => null]);

        $categories = Category::all();
        
        if ($categories->count() < 10) {
            $this->command->info('Not enough categories to create hierarchy.');
            return;
        }

        // 2. Pick 12 random categories to be "Parents" (Main Categories)
        $parents = $categories->random(12);
        
        // 3. Assign the rest as children to these parents
        $children = $categories->diff($parents);
        
        foreach ($children as $child) {
            $parent = $parents->random();
            $child->parent_id = $parent->id;
            $child->save();
        }

        $this->command->info("Hierarchy Fixed: {$parents->count()} Parents, {$children->count()} Children.");
    }
}
