<?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' => false,
                'sort_order' => 0,
            ],
        );

        $hoursModule = MaestroModule::query()->updateOrCreate(
            ['slug' => 'hours'],
            [
                'label' => 'Hour registration',
                'is_enabled' => false,
                'sort_order' => 1,
            ],
        );

        $offersModule = MaestroModule::query()->updateOrCreate(
            ['slug' => 'offers'],
            [
                'label' => 'Offers',
                'is_enabled' => false,
                'sort_order' => 2,
            ],
        );

        $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,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $offersModule->id,
            'sort_order' => 3,
            'key' => 'offers',
            'label' => 'Offers',
            'icon' => 'pi pi-fw pi-file-edit',
            'route' => '/offers',
            'permission' => 'offers.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $hoursModule->id,
            'sort_order' => 4,
            'key' => 'hours-week',
            'label' => 'Hour workbench',
            'icon' => 'pi pi-fw pi-clock',
            'route' => '/hours/week',
            'permission' => 'hours.week.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $hoursModule->id,
            'sort_order' => 5,
            'key' => 'hours-history',
            'label' => 'My hour history',
            'icon' => 'pi pi-fw pi-history',
            'route' => '/hours/history',
            'permission' => 'hours.week.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $hoursModule->id,
            'sort_order' => 6,
            'key' => 'hours-approvals',
            'label' => 'Hour approvals',
            'icon' => 'pi pi-fw pi-check-square',
            'route' => '/hours/approvals',
            'permission' => 'hours.approve',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => $hoursModule->id,
            'sort_order' => 7,
            'key' => 'hours-reports',
            'label' => 'Hour reports',
            'icon' => 'pi pi-fw pi-chart-line',
            'route' => '/hours/reports',
            'permission' => 'hours.reports.view',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        $superadmin = NavigationNode::query()->create([
            'parent_id' => $root->id,
            'module_id' => null,
            'sort_order' => 8,
            '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' => 'Module installation health and activation',
        ]);

        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' => 9,
            '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,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 4,
            'key' => 'settings-offers-catalog',
            'label' => 'Offer catalog',
            'icon' => 'pi pi-fw pi-list',
            'route' => '/settings/offers/catalog',
            'permission' => 'offers.catalog.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 5,
            'key' => 'settings-offers-templates',
            'label' => 'Offer templates',
            'icon' => 'pi pi-fw pi-copy',
            'route' => '/settings/offers/templates',
            'permission' => 'offers.templates.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);
    }
}
