<?php

namespace Database\Seeders;

use App\Models\Menu;
use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class UsersRoleMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $roles = [
            [
                "name" => "superadmin",
                "description" => "show all menus for super admins",
            ],
            [
                "name" => "admin",
                "description" => "show only necessary menus for admins",
            ],
            [
                "name" => "operator",
                "description" => "show only necessary menus for operators",
            ]
        ];

        Role::upsert($roles, ['name']);

        $parent_menus = [
            [
                "name" => "Dashboard",
                "url" => "/dashboard",
                "icon" => "mingcute:home-3-line",
                "parent_id" => null,
                "sort_order" => 1,
            ],
            [
                "name" => "Master",
                "url" => "/master",
                "icon" => "mingcute:cylinder-line",
                "parent_id" => null,
                "sort_order" => 2,
            ],
            [
                "name" => "Settings",
                "url" => "/settings",
                "icon" => "mingcute:settings-6-line",
                "parent_id" => null,
                "sort_order" => 3,
            ],
            [
                "name" => "Data Settings",
                "url" => "/data-settings",
                "icon" => "mingcute:settings-1-line",
                "parent_id" => null,
                "sort_order" => 4,
            ],
            [
                "name" => "Data",
                "url" => "/data",
                "icon" => "mingcute:task-line",
                "parent_id" => null,
                "sort_order" => 5,
            ],
            [
                "name" => "Laporan",
                "url" => "/laporan",
                "icon" => "mingcute:task-line",
                "parent_id" => null,
                "sort_order" => 6,
            ]
        ];

        foreach ($parent_menus as $parent_menu) {
            Menu::firstOrCreate(['name' => $parent_menu['name']], $parent_menu);
        }

        // Attach Menus to Roles
        $superadmin = Role::where('name', 'superadmin')->first();
        $admin = Role::where('name', 'admin')->first();
        $operator = Role::where('name', 'operator')->first();

        $dashboard = Menu::where('name', 'Dashboard')->first();
        $master = Menu::where('name', 'Master')->first();
        $settings = Menu::where('name', 'Settings')->first();
        $dataSettings = Menu::where('name', 'Data Settings')->first();
        $data = Menu::where('name', 'Data')->first();
        $laporan = Menu::where('name', 'Laporan')->first();

        // create children menu
        $children_menus = [
            [
                "name" => "Dashboard Pimpinan",
                "url" => "dashboard.home",
                "icon" => null,
                "parent_id" => $dashboard->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Dashboard PBG",
                "url" => "dashboard.pbg",
                "icon" => null,
                "parent_id" => $dashboard->id,
                "sort_order" => 2,
            ],
            [
                "name" => "Dashboard Potensi",
                "url" => "dashboard.lack_of_potential",
                "icon" => null,
                "parent_id" => $dashboard->id,
                "sort_order" => 3,
            ],
            [
                "name" => "PETA",
                "url" => "dashboard.maps",
                "icon" => null,
                "parent_id" => $dashboard->id,
                "sort_order" => 4,
            ],
            [
                "name" => "Users",
                "url" => "users.index",
                "icon" => null,
                "parent_id" => $master->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Syncronize",
                "url" => "settings.syncronize",
                "icon" => null,
                "parent_id" => $settings->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Menu",
                "url" => "menus.index",
                "icon" => null,
                "parent_id" => $settings->id,
                "sort_order" => 2,
            ],
            [
                "name" => "Role",
                "url" => "roles.index",
                "icon" => null,
                "parent_id" => $settings->id,
                "sort_order" => 3,
            ],
            [
                "name" => "Setting Dashboard",
                "url" => "data-settings.index",
                "icon" => null,
                "parent_id" => $dataSettings->id,
                "sort_order" => 1,
            ],
            [
                "name" => "PBG",
                "url" => "pbg-task.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Reklame",
                "url" => "advertisements.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 2,
            ],
            [
                "name" => "Usaha atau Industri",
                "url" => "business-industries.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 3,
            ],
            [
                "name" => "UMKM",
                "url" => "umkm.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 4,
            ],
            [
                "name" => "Pariwisata",
                "url" => "tourisms.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 5,
            ],
            [
                "name" => "Tata Ruang",
                "url" => "spatial-plannings.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 6,
            ],
            [
                "name" => "PDAM",
                "url" => "customers",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 7,
            ],
            [
                "name" => "Lap Pariwisata",
                "url" => "tourisms.index",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 1,
            ],
        ];

        foreach ($children_menus as $child_menu) {
            Menu::firstOrCreate(['name' => $child_menu['name']], $child_menu);
        }

        $dashboard_pimpinan = Menu::where('name', 'Dashboard Pimpinan')->first();
        $dashboard_pbg = Menu::where('name', 'Dashboard PBG')->first();
        $users = Menu::where('name', 'Users')->first();
        $syncronize = Menu::where('name', 'Syncronize')->first();
        $setting_menu = Menu::where('name', 'Menu')->first();
        $setting_role = Menu::where('name', 'Role')->first();
        $setting_dashboard = Menu::where('name', 'Setting Dashboard')->first();
        $setting_pbg = Menu::where('name', 'PBG')->first();
        $reklame = Menu::where('name', 'Reklame')->first();
        $businessIndustries = Menu::where('name', 'Usaha atau Industri')->first();
        $pariwisata = Menu::where('name', 'Pariwisata')->first();
        $laporan_pariwisata = Menu::where('name', 'Lap Pariwisata')->first();
        $umkm = Menu::where('name', 'UMKM')->first();
        $lack_of_potentials = Menu::where('name', 'Dashboard Potensi')->first();
        $spatial_plannings = Menu::where('name', 'Tata Ruang')->first();
        $pdam = Menu::where('name', 'PDAM')->first();
        $peta = Menu::where('name', 'PETA')->first();

        // Superadmin gets all menus
        $superadmin->menus()->sync([
            // parent
            $dashboard->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $master->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $settings->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $dataSettings->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $data->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $laporan->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            // children
            $dashboard_pimpinan->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $dashboard_pbg->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $users->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $syncronize->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $setting_menu->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $setting_role->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $setting_dashboard->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $setting_pbg->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $reklame->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $businessIndustries->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $pariwisata->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $laporan_pariwisata->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $umkm->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $lack_of_potentials->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $spatial_plannings->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $pdam->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $peta->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
        ]);

        // Admin gets limited menus
        $admin->menus()->sync([
            $dashboard->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $master->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $settings->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
        ]);

        // Operator gets only basic menus with full permissions
        $operator->menus()->sync([
            $dashboard->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $data->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
        ]);
        // Attach User to role super admin
        User::findOrFail(1)->roles()->sync([$superadmin->id]);
    }
}
