<?php

declare(strict_types=1);

namespace Maestro\Core\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;
use Maestro\Core\Models\MaestroModule;
use Maestro\Core\Models\NavigationNode;

class ModuleAndNavigationSeeder extends Seeder
{
    public function run(): void
    {
        $customers = MaestroModule::query()->updateOrCreate(
            ['slug' => 'customers'],
            [
                'label' => 'Customers',
                'is_enabled' => true,
                'sort_order' => 0,
            ],
        );

        Schema::disableForeignKeyConstraints();
        try {
            NavigationNode::query()->delete();
        } finally {
            Schema::enableForeignKeyConstraints();
        }

        $root = NavigationNode::query()->create([
            'parent_id' => null,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'root',
            'label' => 'App',
            'icon' => null,
            'route' => null,
            'permission' => null,
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'dashboard',
            'label' => 'Dashboard',
            'icon' => 'pi pi-fw pi-home',
            'route' => '/dashboard',
            'permission' => null,
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $customers->id,
            'sort_order' => 1,
            'key' => 'customers',
            'label' => 'Customers',
            'icon' => 'pi pi-fw pi-users',
            'route' => '/customers',
            'permission' => null,
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => null,
            'sort_order' => 2,
            'key' => 'superadmin',
            'label' => 'Superadmin',
            'icon' => 'pi pi-fw pi-sliders-h',
            'route' => '/superadmin',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        $settings = NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => null,
            'sort_order' => 3,
            'key' => 'settings',
            'label' => 'Settings',
            'icon' => 'pi pi-fw pi-cog',
            'route' => '/settings',
            'permission' => null,
            'is_header' => false,
            'pinned_bottom' => true,
            'description' => 'Organization, users, roles, and activity log — shown by permission.',
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'settings-organization',
            'label' => 'Organization',
            'icon' => 'pi pi-fw pi-building',
            'route' => '/settings/organization',
            'permission' => 'settings.organization.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 1,
            'key' => 'settings-users',
            'label' => 'Users',
            'icon' => 'pi pi-fw pi-users',
            'route' => '/settings/users',
            'permission' => 'users.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 2,
            'key' => 'settings-roles',
            'label' => 'Roles',
            'icon' => 'pi pi-fw pi-id-card',
            'route' => '/settings/roles',
            'permission' => 'roles.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 3,
            'key' => 'settings-activity-log',
            'label' => 'Activity log',
            'icon' => 'pi pi-fw pi-history',
            'route' => '/settings/activity-log',
            'permission' => 'activitylog.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);
    }
}
