<?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
{
    /**
     * Idempotent bootstrap for fresh installs without running DatabaseSeeder: ensures the
     * Maestro navigation tree exists so SPA menus are never empty solely due to a missing seed step.
     */
    public static function ensureDefaultsPresent(): void
    {
        if (NavigationNode::query()->where('key', 'root')->exists()) {
            return;
        }

        (new self)->seedNavigationTreeWithoutPurge();
    }

    public function run(): void
    {
        $this->purgeNavigationTree();

        $this->seedNavigationTreeWithoutPurge();
    }

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

    private function seedNavigationTreeWithoutPurge(): void
    {
        $customers = MaestroModule::query()->updateOrCreate(
            ['slug' => 'customers'],
            [
                'label' => 'Customers',
                'is_enabled' => true,
                'sort_order' => 0,
            ],
        );

        $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' => 'nav-section-workspace',
            'label' => 'Workspace',
            'icon' => null,
            'route' => null,
            'permission' => null,
            'is_header' => true,
            'pinned_bottom' => false,
            'description' => null,
        ]);

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

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

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'superadmin-modules',
            'label' => 'Modules',
            'icon' => 'pi pi-fw pi-box',
            'route' => '/superadmin/modules',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 1,
            'key' => 'superadmin-navigation',
            'label' => 'Navigation',
            'icon' => 'pi pi-fw pi-sitemap',
            'route' => '/superadmin/navigation',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

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

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 3,
            'key' => 'superadmin-branding',
            'label' => 'Branding',
            'icon' => 'pi pi-fw pi-image',
            'route' => '/superadmin/branding',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        $settings = NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => null,
            'sort_order' => 4,
            '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,
        ]);
    }
}
