<?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:report-line",
                "parent_id" => null,
                "sort_order" => 6,
            ],
            [
                "name" => "Neng Bedas",
                "url" => "/chat",
                "icon" => "mingcute:wechat-line",
                "parent_id" => null,
                "sort_order" => 7,
            ],
            [
                "name" => "Approval",
                "url" => "/approval",
                "icon" => "mingcute:user-follow-2-line",
                "parent_id" => null,
                "sort_order" => 8,
            ],
            [
                "name" => "Tools",
                "url" => "/tools",
                "icon" => "mingcute:tool-line",
                "parent_id" => null,
                "sort_order" => 9,
            ],
        ];

        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();
        $chat_bedas = Menu::where('name', 'Neng Bedas')->first();
        $approval = Menu::where('name', 'Approval')->first();
        $tools = Menu::where('name', 'Tools')->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" => null,
                "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" => "Approval Pejabat",
                "url" => "approval-list",
                "icon" => null,
                "parent_id" => $approval->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" => "web.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" => "web-umkm.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 4,
            ],
            [
                "name" => "Pariwisata",
                "url" => "web-tourisms.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 5,
            ],
            [
                "name" => "Tata Ruang",
                "url" => "web-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" => "Google Sheets",
                "url" => "google-sheets",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 8,
            ],
            [
                "name" => "TPA TPT",
                "url" => "tpa-tpt.index",
                "icon" => null,
                "parent_id" => $data->id,
                "sort_order" => 9,
            ],
            [
                "name" => "Lap Pariwisata",
                "url" => "tourisms-report.index",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Lap Pimpinan",
                "url" => "bigdata-resumes",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 2,
            ],
            [
                "name" => "Rekap Pembayaran",
                "url" => "payment-recaps",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 3,
            ],
            [
                "name" => "Lap Rekap Data Pembayaran",
                "url" => "report-payment-recaps",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 4,
            ],
            [
                "name" => "Lap PBG (PTSP)",
                "url" => "report-pbg-ptsp",
                "icon" => null,
                "parent_id" => $laporan->id,
                "sort_order" => 5,
            ],
            [
                "name" => "Chat",
                "url" => "main-chatbot.index",
                "icon" => null,
                "parent_id" => $chat_bedas->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Luar Sistem",
                "url" => "dashboard.potentials.inside_system",
                "icon" => null,
                "parent_id" => Menu::where('name', 'Dashboard Potensi')->first()->id,
                "sort_order" => 1,
            ],
            [
                "name" => "Dalam Sistem",
                "url" => "dashboard.potentials.outside_system",
                "icon" => null,
                "parent_id" => Menu::where('name', 'Dashboard Potensi')->first()->id,
                "sort_order" => 2,
            ],
            [
                "name" => "Undangan",
                "url" => "invitations",
                "icon" => null,
                "parent_id" => $tools->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();
        $bigdata_resume = Menu::where('name', 'Lap Pimpinan')->first();
        $chatbot = Menu::where('name', 'Chat')->first();
        $dalam_sistem = Menu::where('name', 'Dalam Sistem')->first();
        $luar_sistem = Menu::where('name', 'Luar Sistem')->first();
        $google_sheets = Menu::where('name', 'Google Sheets')->first();
        $tpa_tpt = Menu::where('name', 'TPA TPT')->first();
        $approval_pejabat = Menu::where('name', 'Approval Pejabat')->first();
        $intivations = Menu::where('name', 'Undangan')->first();
        $payment_recap = Menu::where('name', 'Rekap Pembayaran')->first();
        $report_payment_recap = Menu::where('name', 'Lap Rekap Data Pembayaran')->first();
        $report_pbg_ptsp = Menu::where('name', 'Lap PBG (PTSP)')->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],
            $chat_bedas->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $approval->id => ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false],
            $tools->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],
            $dalam_sistem->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $luar_sistem->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $bigdata_resume->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $chatbot->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $google_sheets->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $tpa_tpt->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $approval_pejabat->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $intivations->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $payment_recap->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $report_payment_recap->id => ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true],
            $report_pbg_ptsp->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]);
    }
}
