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

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

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

        $invoicesModule = MaestroModule::query()->updateOrCreate(
            ['slug' => 'invoices'],
            [
                'label' => 'Facturatie',
                'is_enabled' => false,
                'sort_order' => 3,
            ],
        );

        $itemsModule = MaestroModule::query()->updateOrCreate(
            ['slug' => 'items'],
            [
                'label' => 'Artikelen',
                'is_enabled' => false,
                'sort_order' => 4,
            ],
        );

        $root = NavigationNode::query()->create([
            'parent_id' => null,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'root',
            'label' => 'Applicatie',
            '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' => 'Klanten',
            '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' => 2,
            'key' => 'offers',
            'label' => 'Offertes',
            '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' => $invoicesModule->id,
            'sort_order' => 3,
            'key' => 'invoices',
            'label' => 'Facturen',
            'icon' => 'pi pi-fw pi-file',
            'route' => '/invoices',
            'permission' => 'invoices.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' => 'Uren werkplek',
            'icon' => 'pi pi-fw pi-clock',
            'route' => '/hours/week',
            'permission' => 'hours.week.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

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

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'superadmin-installation-health',
            'label' => 'Installatiecontrole',
            'icon' => 'pi pi-fw pi-chart-bar',
            'route' => '/superadmin/installation-health',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => 'Controlelijst (migraties, seed, schakelaars)',
        ]);

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

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 2,
            'key' => 'superadmin-navigation',
            'label' => 'Navigatie',
            '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' => 3,
            'key' => 'superadmin-datatable-settings',
            'label' => 'Tabellen',
            'icon' => 'pi pi-fw pi-table',
            'route' => '/superadmin/datatable-settings',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => 'Standaardkolommen per tabel en gebruikersweergave terugzetten',
        ]);

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 4,
            'key' => 'superadmin-appearance',
            'label' => 'Thema & lay-out',
            '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' => 5,
            'key' => 'superadmin-public-home',
            'label' => 'Publieke homepage',
            'icon' => 'pi pi-fw pi-globe',
            'route' => '/superadmin/public-home',
            'permission' => 'platform.superadmin',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => 'Status en configuratie van de publieke root-pagina (/)',
        ]);

        NavigationNode::query()->create([
            'parent_id' => $superadmin->id,
            'module_id' => null,
            'sort_order' => 6,
            'key' => 'superadmin-branding',
            'label' => 'Huisstijl',
            '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' => 6,
            'key' => 'settings',
            'label' => 'Instellingen',
            'icon' => 'pi pi-fw pi-cog',
            'route' => '/settings',
            'permission' => null,
            'is_header' => false,
            'pinned_bottom' => true,
            'description' => 'Organisatie, gebruikers, rollen en activiteiten — volgens rechten.',
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 0,
            'key' => 'settings-organization',
            'label' => 'Organisatie',
            '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' => 'Gebruikers',
            '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' => 'Rollen',
            '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' => 'Activiteitenlogboek',
            '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' => 'Offertecatalogus',
            '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' => 'Offertesjablonen',
            'icon' => 'pi pi-fw pi-copy',
            'route' => '/settings/offers/templates',
            'permission' => 'offers.templates.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

        NavigationNode::query()->create([
            'parent_id' => $settings->id,
            'module_id' => null,
            'sort_order' => 6,
            'key' => 'settings-invoices-vat',
            'label' => 'BTW-tarieven',
            'icon' => 'pi pi-fw pi-percentage',
            'route' => '/settings/invoices/vat-rates',
            'permission' => 'invoices.vat.manage',
            'is_header' => false,
            'pinned_bottom' => false,
            'description' => null,
        ]);

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