<?php

namespace Database\Seeders;

use App\Models\Permission;
use App\Models\RoleModules;
use App\Models\RolePermission;
use Illuminate\Database\Seeder;
use App\Models\Module;

class ModuleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Module::truncate();
        Permission::truncate();
        RolePermission::truncate();
        RoleModules::truncate();

        $modules = [
            [
                'display_name' => 'Dashboard',
                'name' => 'dashboard',
                'link' => '/admin/dashboard',
                'permissions' => ['view', 'report', 'print'],
                'icon' => 'fa-dashboard'
            ],
            [
                'display_name' => 'SMS Packages',
                'name' => 'pricing',
                'link' => '/admin/pricing',
                'permissions' => ['add', 'update', 'delete', 'view'],
                'icon' => 'fa-cubes'
            ],
            [
                'display_name' => 'RBAC Accesses',
                'name' => 'accesses',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-shield',
                'submenus' => [
                    [
                        'display_name' => 'Modules',
                        'name' => 'module',
                        'link' => '/admin/module',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Roles',
                        'name' => 'role',
                        'link' => '/admin/role',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Module Permissions',
                        'name' => 'module_permission',
                        'link' => '/admin/module_permission',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Role Permissions',
                        'name' => 'role_permission',
                        'link' => '/admin/role_permissions',
                        'permissions' => ['add', 'delete'],
                    ],
                    [
                        'display_name' => 'Users',
                        'name' => 'user',
                        'link' => '/admin/users',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                        'submenus' => []
                    ],
                ]
            ],
            [
                'display_name' => 'Settings',
                'name' => 'setting',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-cog',
                'submenus' => [
                    [
                        'display_name' => 'Configurations',
                        'name' => 'configuration',
                        'link' => '/admin/configurations',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'SMS Packages',
                        'name' => 'package_setting',
                        'link' => '/admin/pricing',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Operators',
                        'name' => 'operator',
                        'link' => '/admin/operators',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Operator Number',
                        'name' => 'operator_number',
                        'link' => '/admin/operator_numbers',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Sender Types',
                        'name' => 'sender_type',
                        'link' => '/admin/sender_types',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Message Apis (CLI)',
                        'name' => 'sender_id',
                        'link' => '/admin/sender_id',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Ticket Category',
                        'name' => 'ticket_category',
                        'link' => '/admin/ticket_category',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'User Devices',
                        'name' => 'user_devices',
                        'link' => '/admin/user_devices',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                ]
            ],
            [
                'display_name' => 'Messaging',
                'name' => 'messaging',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-envelope',
                'submenus' => [
                    [
                        'display_name' => 'Quick SMS',
                        'name' => 'quick_sms',
                        'link' => '/admin/sms_messages',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Schedule SMS',
                        'name' => 'schedule_sms',
                        'link' => '/admin/schedule_sms',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'File2SMS',
                        'name' => 'file2_sms',
                        'link' => '/admin/file2_sms',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                ]
            ],
            [
                'display_name' => 'Reports',
                'name' => 'reports',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-file',
                'submenus' => [
                    [
                        'display_name' => 'SMS Log',
                        'name' => 'sms_log',
                        'link' => '/admin/sms_log',
                        'permissions' => ['view'],
                    ],
                    [
                        'display_name' => 'History Log',
                        'name' => 'history_log',
                        'link' => '/admin/history_log',
                        'permissions' => ['view'],
                    ],
                    [
                        'display_name' => 'Failed Log',
                        'name' => 'failed_log',
                        'link' => '/admin/failed_logs',
                        'permissions' => ['view',],
                    ],
                    [
                        'display_name' => 'User Wise Details',
                        'name' => 'sms_details',
                        'link' => '/admin/sms_details',
                        'permissions' => ['view',],
                    ],
                    [
                        'display_name' => 'User Wise Recharge Report',
                        'name' => 'user_wise_recharge',
                        'link' => '/admin/user_wise_recharge',
                        'permissions' => ['view'],
                    ],
                    [
                        'display_name' => 'Coasting Summery',
                        'name' => 'coasting_summery',
                        'link' => '/admin/coasting_summery',
                        'permissions' => ['view'],
                    ],
                    [
                        'display_name' => 'A2P Revenue Sharing',
                        'name' => 'revenue_sharing',
                        'link' => '/admin/revenue_sharing',
                        'permissions' => ['view'],
                    ],
                ]
            ],
            [
                'display_name' => 'Phone Book',
                'name' => 'phone_book',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-address-book',
                'submenus' => [
                    [
                        'display_name' => 'Group List',
                        'name' => 'group_list',
                        'link' => '/admin/groups',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Campaign List',
                        'name' => 'campaign_list',
                        'link' => '/admin/campaigns',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                ]
            ],
            [
                'display_name' => 'Payment',
                'name' => 'payment',
                'link' => '#',
                'permissions' => ['show'],
                'icon' => 'fa-money-check-alt',
                'submenus' => [
                    [
                        'display_name' => 'Buy SMS & Packages',
                        'name' => 'buy_sms',
                        'link' => '/admin/buy_sms',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Payment Requests',
                        'name' => 'payment_request',
                        'link' => '/admin/payment_request',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                    [
                        'display_name' => 'Payment Logs',
                        'name' => 'payment_log',
                        'link' => '/admin/payment_log',
                        'permissions' => ['add', 'update', 'delete', 'view'],
                    ],
                ]
            ],
            [
                'display_name' => 'Developers',
                'name' => 'developers',
                'link' => '/admin/developers',
                'permissions' => ['add', 'update', 'delete', 'view'],
                'icon' => 'fa-file-code',
            ],
            [
                'display_name' => 'Contact From Website',
                'name' => 'contact_from_website',
                'link' => '/admin/contact_from_website',
                'permissions' => ['add', 'update', 'delete', 'view'],
                'icon' => 'fa-phone',
            ],
        ];

        foreach ($modules as $data) {
            $submenus = isset($data['submenus']) && count($data['submenus']) > 0 ? $data['submenus'] : [];
            $permissions = $data['permissions'];
            unset($data['submenus']);
            unset($data['permissions']);

            $parentModule = $this->insertModule($data, 0);
            $this->insertRoleModule($parentModule->id, 1);

            foreach ($permissions as $permission) {
                $new_permission = $this->insertPermission($parentModule->name, $permission, $parentModule->display_name, $parentModule->id);
                $this->insertRolePermission($new_permission->id, 1);
            }

            if (count($submenus) > 0) {
                foreach ($submenus as $submenu) {
                    $subSubMenus = isset($submenu['submenus']) && count($submenu['submenus']) > 0 ? $submenu['submenus'] : [];

                    $permissions = $submenu['permissions'];
                    unset($submenu['permissions']);
                    unset($submenu['submenus']);

                    $module = $this->insertModule($submenu, $parentModule->id, 1);
                    $subParent = $module;

                    $this->insertRoleModule($module->id, 1);

                    foreach ($permissions as $permission) {
                        $new_permission = $this->insertPermission($module->name, $permission, $module->display_name, $module->id);
                        $this->insertRolePermission($new_permission->id, 1);
                    }

                    if (count($subSubMenus) > 0) {
                        foreach ($subSubMenus as $subSubMenu) {
                            $permissions = $subSubMenu['permissions'];
                            unset($subSubMenu['permissions']);
                            unset($subSubMenu['submenus']);

                            $module = $this->insertModule($subSubMenu, $subParent->id);
                            $this->insertRoleModule($module->id, 1);

                            foreach ($permissions as $permission) {
                                $new_permission = $this->insertPermission($module->name, $permission, $module->display_name, $module->id);
                                $this->insertRolePermission($new_permission->id, 1);
                            }
                        }
                    }
                }
            }
        }
    }

    public function insertModule($data, $parent_id = 0)
    {
        $module = new Module();
        $module->fill($data);
        $module->parent_id = $parent_id;
        $module->save();

        return $module;
    }

    public function insertRoleModule($module_id, $role_id = 1)
    {
        $role_module = new RoleModules();
        $role_module->role_id = $role_id;
        $role_module->module_id = $module_id;
        $role_module->save();

        return $role_module;
    }

    public function insertPermission($name, $permission, $display_name, $module_id)
    {
        $new_name = $name . '_' . $permission;
        $new_permission = new Permission();
        $new_permission->module_id = $module_id;
        $new_permission->name = $new_name;
        $new_permission->display_name = $display_name . ' ' . str_replace('_', ' ', $permission);
        $new_permission->save();

        return $new_permission;
    }

    public function insertRolePermission($permission_id, $role_id = 1)
    {
        $role_module = new RolePermission();
        $role_module->role_id = $role_id;
        $role_module->permission_id = $permission_id;
        $role_module->save();

        return $role_module;
    }
}
