<?php

namespace Database\Seeders;

use App\Modules\Catalog\Models\Category;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

/**
 * CategorySeeder — seeds a root category tree for development.
 */
class CategorySeeder extends Seeder
{
    public function run(): void
    {
        $tree = [
            ['en' => 'Electronics',  'ne' => 'इलेक्ट्रोनिक्स', 'children' => [
                ['en' => 'Smartphones',  'ne' => 'स्मार्टफोन'],
                ['en' => 'Laptops',      'ne' => 'ल्यापटप'],
                ['en' => 'Accessories',  'ne' => 'सामान'],
            ]],
            ['en' => 'Fashion',      'ne' => 'फेसन', 'children' => [
                ['en' => 'Men\'s Clothing', 'ne' => 'पुरुष पोशाक'],
                ['en' => 'Women\'s Clothing', 'ne' => 'महिला पोशाक'],
                ['en' => 'Footwear',     'ne' => 'जुत्ता'],
            ]],
            ['en' => 'Home & Living', 'ne' => 'घर र जीवन', 'children' => [
                ['en' => 'Furniture',   'ne' => 'फर्निचर'],
                ['en' => 'Kitchen',     'ne' => 'भान्सा'],
            ]],
            ['en' => 'Books',        'ne' => 'किताब'],
            ['en' => 'Sports',       'ne' => 'खेलकुद'],
        ];

        foreach ($tree as $sort => $catData) {
            $parent = Category::firstOrCreate(
                ['slug' => Str::slug($catData['en'])],
                [
                    'name'         => ['en' => $catData['en'], 'ne' => $catData['ne']],
                    'depth'        => 0,
                    'sort_order'   => $sort,
                    'is_active'    => true,
                    'show_in_menu' => true,
                ]
            );

            foreach ($catData['children'] ?? [] as $childSort => $childData) {
                Category::firstOrCreate(
                    ['slug' => Str::slug($childData['en'])],
                    [
                        'parent_id'    => $parent->id,
                        'name'         => ['en' => $childData['en'], 'ne' => $childData['ne']],
                        'depth'        => 1,
                        'path'         => (string) $parent->id,
                        'sort_order'   => $childSort,
                        'is_active'    => true,
                        'show_in_menu' => true,
                    ]
                );
            }
        }

        $this->command->info('✅ Category tree seeded.');
    }
}
