<?php

namespace Database\Seeders;

use App\Models\Group;
use App\Models\Menu;
use App\Models\MenuType;
use Illuminate\Database\Seeder;

class MenuSeeder extends Seeder
{
    /**
     * Seed katalog menu default dari NavigationService (RPD FR-USR-05/06).
     */
    public function run(): void
    {
        $sideType = MenuType::firstOrCreate(['type' => 'Side menu']);

        $adminGroups = [Group::ADMIN, Group::DEVELOPER];
        $allGroups = [Group::ADMIN, Group::MEMBERS, Group::DEVELOPER];

        // [section, label, route, icon, groups]
        $catalog = [
            ['Utama', 'Dashboard', 'dashboard', 'layout-grid', $allGroups],
            ['Iklan', 'Campaign', 'campaigns.index', 'megaphone', $allGroups],
            ['Iklan', 'Draft', 'campaigns.drafts', 'file-text', $allGroups],
            ['Iklan', 'Kredit', 'credits.index', 'coins', $allGroups],
            ['Iklan', 'Billing', 'billings.index', 'receipt', $allGroups],
            ['Master', 'Kategori Landing', 'kategoris.index', 'layers', $adminGroups],
            ['Master', 'Ads Placement', 'placements.index', 'map-pin', $adminGroups],
            ['Master', 'Ad Platform', 'ad-platforms.index', 'layout-list', $adminGroups],
            ['Master', 'Bank', 'banks.index', 'landmark', $adminGroups],
            ['Sistem', 'Pengguna', 'users.index', 'users', $adminGroups],
            ['Sistem', 'Grup', 'groups.index', 'shield', $adminGroups],
            ['Sistem', 'Menu', 'menus.index', 'menu', $adminGroups],
            ['Sistem', 'Pengaturan', 'admin-settings.edit', 'settings', $adminGroups],
            ['Pengaturan', 'Profil', 'profile.edit', 'user-cog', $allGroups],
            ['Pengaturan', 'Keamanan', 'security.edit', 'lock', $allGroups],
            ['Pengaturan', 'Tampilan', 'appearance.edit', 'palette', $allGroups],
        ];

        $order = 0;
        foreach ($catalog as $item) {
            [$section, $label, $route, $icon, $groups] = $item;

            $menu = Menu::firstOrCreate(
                ['route' => $route, 'label' => $label],
                [
                    'section' => $section,
                    'route' => $route,
                    'link' => null,
                    'icon' => $icon,
                    'menu_type_id' => $sideType->id,
                    'parent_id' => null,
                    'sort_order' => $order++,
                    'status' => true,
                ]
            );

            $groupIds = Group::whereIn('name', $groups)->pluck('id')->all();
            $menu->groups()->syncWithoutDetaching($groupIds);
        }
    }
}
