<?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 = $this->upsertModule('customers', 'Klanten', 0);
        $hoursModule = $this->upsertModule('hours', 'Urenregistratie', 1);
        $offersModule = $this->upsertModule('offers', 'Offertes', 2);
        $invoicesModule = $this->upsertModule('invoices', 'Facturatie', 3);
        $itemsModule = $this->upsertModule('items', 'Artikelen', 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' => $offersModule->id,
            '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' => $offersModule->id,
            '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' => $invoicesModule->id,
            '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,
        ]);
    }

    private function upsertModule(string $slug, string $label, int $sortOrder): MaestroModule
    {
        $defaults = [
            'label' => $label,
            'is_enabled' => false,
            'sort_order' => $sortOrder,
        ];
        if (Schema::hasColumn('maestro_modules', 'is_install_enabled')) {
            $defaults['is_install_enabled'] = false;
        }

        $module = MaestroModule::query()->firstOrCreate(
            ['slug' => $slug],
            $defaults,
        );

        $module->fill([
            'label' => $label,
            'sort_order' => $sortOrder,
        ]);

        if ($module->isDirty(['label', 'sort_order'])) {
            $module->save();
        }

        return $module;
    }
}
