<?php

namespace Database\Seeders;

use App\Models\Menu;
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $no = 1;
        $aArrMenu = [
            'dashboard',
            // 'data',
            'master',
            'management',
            'setting',
            // 'headNav',
        ];

        for ($i = 0; $i < count($aArrMenu); $i++) {
            $menuNumber = 1;
            $this->{$aArrMenu[$i]}($i + 1, $menuNumber);
            $this->command->info($aArrMenu[$i]);
        }
        $this->assignRoleMenu();
    }

    private function dashboard($id, $menuNumber)
    {
        $save['id']    = $id;
        $save['name']  = 'Dashboard';
        $save['tipe']  = 'main';
        $save['slug']  = 'dashboard_~|role|~';
        $save['url']   = 'dashboard/~|role|~';
        $save['route'] = 'dashboard';
        $save['order'] = $menuNumber;
        $save['level'] = '0';
        $save['view_path']  = 'dashboard/~|role|~';
        // $save['view_file']  = 'default';
        Menu::create($save);
    }

    private function master($id, $menuNumber)
    {
        $dd = 1;
        $save['id']    = $id;
        $save['name']  = 'Master';
        $save['slug']  = 'master';
        $save['url']   = 'master';
        $save['route'] = 'master';
        $save['order'] = $menuNumber;
        $save['level'] = '0';
        Menu::create($save);

        $menuNumber = 1;
        $save['id']         = $id . $dd++;
        $save['parent_id']  = $id;
        $save['name']  = 'Kompetensi Keahlian';
        $save['slug']  = 'kompahli';
        $save['url']   = 'master/kompahli';
        $save['route'] = 'master';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'master/kompetensi_keahlian';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];
        $this->actionMenu($roleMenu, $permissionSlugs);

        $menuNumber = 1;
        $save['id']         = $id . $dd++;
        $save['parent_id']  = $id;
        $save['name']  = 'Kelas';
        $save['slug']  = 'kelas';
        $save['url']   = 'master/kelas';
        $save['route'] = 'master';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'master/kelas';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];
        $this->actionMenu($roleMenu, $permissionSlugs);
    }

    private function management($id, $menuNumber)
    {
        $dd = $id;
        $save['id']    = $id;
        $save['name']  = 'Management';
        $save['slug']  = 'management';
        $save['url']   = 'management';
        $save['route'] = 'management';
        $save['order'] = $menuNumber;
        $save['level'] = '0';
        Menu::create($save);

        $menuNumber = 1;
        $save['id']         = $id . $dd;
        $save['parent_id']  = $id;
        $save['name']  = 'Management Siswa';
        $save['slug']  = 'mansi';
        $save['url']   = 'management/mansi';
        $save['route'] = 'management';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'management/siswa';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];
        $this->actionMenu($roleMenu, $permissionSlugs);
    }

    private function setting($id, $menuNumber)
    {
        $dd = 1;
        $save['id']    = $id;
        $save['name']  = 'Setting';
        $save['slug']  = 'setting';
        $save['url']   = 'setting';
        $save['route'] = 'setting';
        $save['order'] = $menuNumber;
        $save['level'] = '0';
        Menu::create($save);

        $menuNumber = 1;
        $save['id']         = $id . $dd++;
        $save['parent_id']  = $id;
        $save['name']  = 'Data Lembaga';
        $save['slug']  = 'dalem';
        $save['url']   = 'setting/dalem';
        $save['route'] = 'setting';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'setting/sekolah';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];

        $this->actionMenu($roleMenu, $permissionSlugs);

        $save['id']         = $id . $dd++;
        $save['parent_id']  = $id;
        $save['name']  = 'Kelulusan';
        $save['slug']  = 'setlu';
        $save['url']   = 'setting/dalem';
        $save['route'] = 'setting';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'setting/kelulusan';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];
        $this->actionMenu($roleMenu, $permissionSlugs);

        $save['id']         = $id . $dd++;
        $save['parent_id']  = $id;
        $save['name']  = 'Upload Siswa';
        $save['slug']  = 'sups';
        $save['url']   = 'setting/sups';
        $save['route'] = 'setting';
        $save['order'] = $menuNumber++;
        $save['level'] = '1';
        $save['view_path']  = 'setting/upload/siswa';
        $roleMenu = Menu::create($save);

        $permissionSlugs = ['create', 'edit', 'delete', 'read'];
        $this->actionMenu($roleMenu, $permissionSlugs);
    }


    private function actionMenu($roleMenu, $permissionSlugs)
    {
        // Buat permission otomatis untuk menu Role Management
        $permissions = [];

        foreach ($permissionSlugs as $slug) {
            $permissions[] = [
                'name' => ucfirst($slug) . ' ' . $roleMenu->name,
                'slug' => "{$slug}_" . $roleMenu->slug,
                'menu_id' => $roleMenu->id,
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }

        Permission::insert($permissions);
    }

    public function assignRoleMenu()
    {
        $aArrRoles = Role::all();
        foreach ($aArrRoles as $role) {
            if (method_exists($this, $role->slug)) {
                $this->{$role->slug}($role);
            }
        }
    }

    private function admin_sekolah($role)
    {
        $menuIds = Menu::all()->pluck('id')->toArray();
        if (!empty($menuIds)) {
            $this->command->info($role->name);
            $role->menus()->syncWithoutDetaching($menuIds);
        }
    }

    private function siswa($role)
    {
        $inMenu = $this->global();
        // $inMenu = array_merge($inMenu, ['ijin_siswa_pkl']);
        $menuIds = Menu::whereIn('slug', $inMenu)->pluck('id')->toArray();

        if (!empty($menuIds)) {
            $this->command->info($role->name);
            $role->menus()->syncWithoutDetaching($menuIds);
        }
    }

    private function global()
    {
        $inMenu[] = 'dashboard_~|role|~';
        // $inMenu[] = 'profile_~|role|~';
        return  $inMenu;
    }
}
