<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionSeeder extends Seeder
{
    // php artisan db:seed --class=PermissionSeeder
    public function run()
    {
        $permissions = [
            ['id' => '1',  'name' => 'Dashboard',                           'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '2',  'name' => 'Total Loan Disbursement',             'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '3',  'name' => 'Total Principal Paid',                'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '4',  'name' => 'Total Received',                      'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '5',  'name' => 'Balance Principal',                   'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '6',  'name' => 'Total Clients',                       'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '7',  'name' => 'Total Client Late',                   'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '8',  'name' => 'Total Client Disbursement Today',     'guard_name'=> 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '9',  'name' => 'Total Income',                        'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '10', 'name' => 'Total Expense',                       'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '11', 'name' => 'Net Profit / Loss',                   'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '12', 'name' => 'Total Loan Disbursed Daily',          'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '13', 'name' => 'Total Client Detail',                 'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '14', 'name' => 'Income / Expense',                    'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
            ['id' => '15', 'name' => 'Daily Repay',                         'guard_name' => 'web', 'group_prefix' => 'Dashboard'],
           
                // Currency
            ['id' => '16', 'name' => 'Currency List',                       'guard_name' => 'web','group_prefix' => 'Currency'],
            ['id' => '17', 'name' => 'Currency Create',                     'guard_name' => 'web','group_prefix' => 'Currency'],
            ['id' => '18', 'name' => 'Currency Edit',                       'guard_name' => 'web','group_prefix' => 'Currency'],
            ['id' => '19', 'name' => 'Currency Delete',                     'guard_name' => 'web','group_prefix' => 'Currency'],

            // Holiday              
            ['id' => '20', 'name' => 'Holiday List',                        'guard_name' => 'web','group_prefix' => 'Holiday'],
            ['id' => '21', 'name' => 'Holiday Create',                      'guard_name' => 'web','group_prefix' => 'Holiday'],
            ['id' => '22', 'name' => 'Holiday Edit',                        'guard_name' => 'web','group_prefix' => 'Holiday'],
            ['id' => '23', 'name' => 'Holiday Delete',                      'guard_name' => 'web','group_prefix' => 'Holiday'],

            // Settings             
            ['id' => '24', 'name' => 'Settings',                            'guard_name' => 'web','group_prefix' => 'Settings'],
            ['id' => '25', 'name' => 'Backup',                              'guard_name' => 'web','group_prefix' => 'Settings'],
            ['id' => '26', 'name' => 'Company List',                        'guard_name' => 'web','group_prefix' => 'Settings'],
            ['id' => '27', 'name' => 'Logactivity List',                    'guard_name' => 'web','group_prefix' => 'Settings'],

            // Users                
            ['id' => '28', 'name' => 'User List',                           'guard_name' => 'web','group_prefix' => 'Users'],
            ['id' => '29', 'name' => 'User Create',                         'guard_name' => 'web','group_prefix' => 'Users'],
            ['id' => '30', 'name' => 'User Edit',                           'guard_name' => 'web','group_prefix' => 'Users'],
            ['id' => '31', 'name' => 'User Delete',                         'guard_name' => 'web','group_prefix' => 'Users'],

            // User Roles               
            ['id' => '32', 'name' => 'User Role List',                      'guard_name' => 'web','group_prefix' => 'User Roles'],
            ['id' => '33', 'name' => 'User Role Create',                    'guard_name' => 'web','group_prefix' => 'User Roles'],
            ['id' => '34', 'name' => 'User Role Edit',                      'guard_name' => 'web','group_prefix' => 'User Roles'],
            ['id' => '35', 'name' => 'User Role Delete',                    'guard_name' => 'web','group_prefix' => 'User Roles'],

            // Clients              
            ['id' => '36', 'name' => 'Client List',                         'guard_name' => 'web','group_prefix' => 'Clients'],
            ['id' => '37', 'name' => 'Client Create',                       'guard_name' => 'web','group_prefix' => 'Clients'],
            ['id' => '38', 'name' => 'Client Edit',                         'guard_name' => 'web','group_prefix' => 'Clients'],
            ['id' => '39', 'name' => 'Client Delete',                       'guard_name' => 'web','group_prefix' => 'Clients'],

            // Credit Officer               
            ['id' => '40', 'name' => 'Co List',                             'guard_name' => 'web','group_prefix' => 'Credit Officer'],
            ['id' => '41', 'name' => 'Co Create',                           'guard_name' => 'web','group_prefix' => 'Credit Officer'],
            ['id' => '42', 'name' => 'Co Edit',                             'guard_name' => 'web','group_prefix' => 'Credit Officer'],
            ['id' => '43', 'name' => 'Co Delete',                           'guard_name' => 'web','group_prefix' => 'Credit Officer'],

            // Collateral               
            ['id' => '44', 'name' => 'Collateral List',                     'guard_name' => 'web','group_prefix' => 'Collateral'],
            ['id' => '45', 'name' => 'Collateral Create',                   'guard_name' => 'web','group_prefix' => 'Collateral'],
            ['id' => '46', 'name' => 'Collateral Edit',                     'guard_name' => 'web','group_prefix' => 'Collateral'],
            ['id' => '47', 'name' => 'Collateral Delete',                   'guard_name' => 'web','group_prefix' => 'Collateral'],

            // Loans 
            ['id' => '48', 'name' => 'List of Client Disbursed',            'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '49', 'name' => 'Loan Create',                         'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '50', 'name' => 'Loan Edit',                           'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '51', 'name' => 'Loan Reschedule',                     'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '52', 'name' => 'Loan Credit Card',                    'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '53', 'name' => 'Loan Delete',                         'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '54', 'name' => 'Contract Loan',                       'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '55', 'name' => 'Loan Table Schedule',                 'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '56', 'name' => 'Loan Approve',                        'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '57', 'name' => 'List Request CO',                     'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '58', 'name' => 'History Payment',                     'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '59', 'name' => 'List of Loan Disbursement',           'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '60', 'name' => 'List Request',                        'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '61', 'name' => 'Move CO',                             'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '62', 'name' => 'Move To Write Off',                   'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '63', 'name' => 'Write Off List',                      'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '64', 'name' => 'Loan Collateral Create',              'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '65', 'name' => 'Loan Request CO',                     'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '66', 'name' => 'Loan Request BM',                     'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '67', 'name' => 'Loan Approve',                        'guard_name' =>'web','group_prefix' => 'Loans'],
            ['id' => '68', 'name' => 'List Request BM',                     'guard_name' => 'web','group_prefix' => 'Loans'],
            ['id' => '69', 'name' => 'Transfer Loan To Branch',             'guard_name' => 'web','group_prefix' => 'Loans'],

            // Payments
            ['id' => '70', 'name' => 'Payment List',                         'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '71', 'name' => 'Repay',                                'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '72', 'name' => 'Pay Off',                              'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '73', 'name' => 'Repay Edit',                           'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '74', 'name' => 'Repay Delete',                         'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '75', 'name' => 'Collect Good List',                    'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '76', 'name' => 'Collect Late List',                    'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '77', 'name' => 'Collect Write Off List',               'guard_name' => 'web','group_prefix' => 'Payments'],
            ['id' => '78', 'name' => 'Client Payment List',                  'guard_name' => 'web','group_prefix' => 'Payments'],

            // Client Payments
            ['id' => '79', 'name' => 'Client Payment List', 'guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '80', 'name' => 'Co Received Report', 'guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '81', 'name' => 'Cashier Received Report','guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '82', 'name' => 'Closing Report', 'guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '83', 'name' => 'Submit Closing Report', 'guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '84', 'name' => 'Edit Closing Report', 'guard_name' => 'web','group_prefix' => 'Client Payments'],

            // Reports
            ['id' => '85', 'name' => 'Credit Officer Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '86', 'name' => 'Daily Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '87', 'name' => 'Loan Repayment Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '88', 'name' => 'Loan Arrear Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '89', 'name' => 'Borrow Report','guard_name' => 'web','group_prefix'=> 'Reports'],
            ['id' => '90', 'name' => 'Overdue Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '91', 'name' => 'Collected Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '92', 'name' => 'Loan Pay Off Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '93', 'name' => 'Summary Co Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '94', 'name' => 'Collateral Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '95', 'name' => 'Loan Approval/Rejected Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '96', 'name' => 'Loan Disbursement Report','guard_name' => 'web','group_prefix' => 'Reports'],
            ['id' => '97', 'name' => 'Summary Productivity By Branch','guard_name' => 'web','group_prefix' => 'Reports'],

            // Type Account
            ['id' => '98', 'name' => 'Type Account List','guard_name' => 'web','group_prefix' => 'Type Account'],
            ['id' => '99', 'name' => 'Type Account Create','guard_name' => 'web','group_prefix' => 'Type Account'],
            ['id' => '100', 'name' => 'Type Account Edit','guard_name' => 'web','group_prefix' => 'Type Account'],
            ['id' => '101', 'name' => 'Type Account Delete','guard_name' => 'web','group_prefix' => 'Type Account'],

            // Chart of Account
            ['id' => '102', 'name' => 'ChartOfAccount List','guard_name' => 'web','group_prefix' => 'Chart Of Account'],
            ['id' => '103', 'name' => 'ChartOfAccount Create','guard_name' => 'web','group_prefix' => 'Chart Of Account'],
            ['id' => '104', 'name' => 'ChartOfAccount Edit','guard_name' => 'web','group_prefix' => 'Chart Of Account'],
            ['id' => '105', 'name' => 'ChartOfAccount Delete','guard_name' => 'web','group_prefix' => 'Chart Of Account'],

            // Asset
            ['id' => '106','name' => 'Asset List','guard_name' => 'web','group_prefix' => 'Asset'],
            ['id' => '107','name' => 'Asset Create','guard_name' => 'web','group_prefix' => 'Asset'],
            ['id' => '108','name' => 'Asset Edit','guard_name' => 'web','group_prefix' => 'Asset'],
            ['id' => '109','name' => 'Asset Delete','guard_name' => 'web','group_prefix' => 'Asset'],

            // Depreciation
            ['id' => '110','name' => 'Depreciation List','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '111','name' => 'Depreciation Create','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '112','name' => 'Depreciation Edit','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '113','name' => 'Depreciation Show','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '114','name' => 'Depreciation Print','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '115','name' => 'Depreciation Delete','guard_name' => 'web','group_prefix' => 'Depreciation'],
            ['id' => '116','name' => 'Summary Asset Depreciation Report','guard_name' => 'web','group_prefix' => 'Depreciation'],

            // Expense
            ['id' => '117','name' => 'Expense List','guard_name' => 'web','group_prefix' => 'Expense'],
            ['id' => '118','name' => 'Expense Create','guard_name' => 'web','group_prefix' => 'Expense'],
            ['id' => '119','name' => 'Expense Edit','guard_name' => 'web','group_prefix' => 'Expense'],
            ['id' => '120','name' => 'Expense Delete','guard_name' => 'web','group_prefix' => 'Expense'],

            // Income
            ['id' => '121','name' => 'Income List','guard_name' => 'web','group_prefix' => 'Income'],
            ['id' => '122','name' => 'Income Create','guard_name' => 'web','group_prefix' => 'Income'],
            ['id' => '123','name' => 'Income Edit','guard_name' => 'web','group_prefix' => 'Income'],
            ['id' => '124','name' => 'Income Delete','guard_name' => 'web','group_prefix' => 'Income'],

            // Journals
            ['id' => '125','name' => 'Journal List','guard_name' => 'web','group_prefix' => 'Journals'],
            ['id' => '126','name' => 'Journal Create','guard_name' => 'web','group_prefix' => 'Journals'],
            ['id' => '127','name' => 'Journal Edit','guard_name' => 'web','group_prefix' => 'Journals'],
            ['id' => '128','name' => 'Journal Delete','guard_name' => 'web','group_prefix' => 'Journals'],

            // Financial Reports
            ['id' => '129','name' => 'Expense Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '130','name' => 'Income Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '131','name' => 'Journal Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '132','name' => 'Net Loss Profit','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '133','name' => 'TrialBalance Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '134','name' => 'GeneralLedger Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '135','name' => 'CashFlow Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '136','name' => 'BalanceSheet Report','guard_name' => 'web','group_prefix' => 'Financial Reports'],
            ['id' => '137','name' => 'PROFIT AND LOSS STATEMENT BY MONTH','guard_name' => 'web','group_prefix' => 'Financial Reports'],

            // Saving
            ['id' => '138','name' => 'Saving List','guard_name' => 'web','group_prefix' => 'Saving'],
            ['id' => '139','name' => 'Saving Request','guard_name' => 'web','group_prefix' => 'Saving'],
            ['id' => '140','name' => 'Saving Approve','guard_name' => 'web','group_prefix' => 'Saving'],
            ['id' => '141','name' => 'Saving Reject','guard_name' => 'web','group_prefix' => 'Saving'],

            // Branch
            ['id' => '142','name' => 'Branch List','guard_name' => 'web','group_prefix' => 'Branch'],
            ['id' => '143','name' => 'Branch Create','guard_name' => 'web','group_prefix' => 'Branch'],
            ['id' => '144','name' => 'Branch Edit','guard_name' => 'web','group_prefix' => 'Branch'],
            ['id' => '145','name' => 'Branch Delete','guard_name' => 'web','group_prefix' => 'Branch'],

            // Banknote Management
            ['id' => '146','name' => 'Daily Banknote Management','guard_name' => 'web','group_prefix' => 'Banknote Management'],
            ['id' => '147','name' => 'Banknote Management List','guard_name' => 'web','group_prefix' => 'Banknote Management'],
            ['id' => '148','name' => 'Banknote Management Create','guard_name' => 'web','group_prefix' => 'Banknote Management'],
            ['id' => '149','name' => 'Banknote Management Edit','guard_name' => 'web','group_prefix' => 'Banknote Management'],
            ['id' => '150','name' => 'Banknote Management Print','guard_name' => 'web','group_prefix' => 'Banknote Management'],
            ['id' => '151','name' => 'Banknote Management Delete','guard_name' => 'web','group_prefix' => 'Banknote Management'],

            // Manage Group CO
            ['id' => '152','name' => 'Manage Group CO List','guard_name' => 'web','group_prefix' => 'Manage Group CO'],
            ['id' => '153','name' => 'Manage Group CO Create','guard_name' => 'web','group_prefix' => 'Manage Group CO'],
            ['id' => '154','name' => 'Manage Group CO Edit','guard_name' => 'web','group_prefix' => 'Manage Group CO'],
            ['id' => '155','name' => 'Manage Group CO Delete','guard_name' => 'web','group_prefix' => 'Manage Group CO'],

            // Cash Transfer Request
            ['id' => '156','name' => 'Cash Transfer Request List','guard_name' => 'web','group_prefix' => 'Cash Transfer Request'],
            ['id' => '157','name' => 'Cash Transfer Request Create','guard_name' => 'web','group_prefix' => 'Cash Transfer Request'],
            ['id' => '158','name' => 'Cash Transfer Request Edit','guard_name' => 'web','group_prefix' => 'Cash Transfer Request'],
            ['id' => '159','name' => 'Cash Transfer Request Delete','guard_name' => 'web','group_prefix' => 'Cash Transfer Request'],

            ['id' => '160','name' => 'Show Payment', 'guard_name' => 'web','group_prefix' => 'Loans'],

            ['id' => '161', 'name' => 'Client Payment On The Day', 'guard_name' => 'web','group_prefix' => 'Client Payments'],
            ['id' => '162','name' => 'Move Write Off To Loan', 'guard_name' => 'web','group_prefix' => 'Loans'],
        ];
        
        foreach ($permissions as  $value) {
            Permission::updateOrCreate(['id' => $value['id']], [
                'name' => $value['name'],
                'group_prefix' => $value['group_prefix'],
            ]);
        }

        $managing_director = [
            ['id' => 19],
            ['id' => 23],
            ['id' => 31],
            ['id' => 35],
            ['id' => 39],
            ['id' => 43],
            ['id' => 47],
            ['id' => 101],
            ['id' => 105],
            ['id' => 109],
            ['id' => 115],
            ['id' => 120],
            ['id' => 124],
            ['id' => 128],
            ['id' => 141],
            ['id' => 145],
            ['id' => 151],
            ['id' => 155],
        ];
        $bm = [
            ['id' => 1],
            ['id' => 2],
            ['id' => 3],
            ['id' => 4],
            ['id' => 5],
            ['id' => 6],
            ['id' => 7],
            ['id' => 8],
            ['id' => 9],
            ['id' => 10],
            ['id' => 11],
            ['id' => 12],
            ['id' => 13],
            ['id' => 14],
            ['id' => 15],
            ['id' => 16],
            ['id' => 17],
            ['id' => 18],
            ['id' => 19],
            ['id' => 20],
            ['id' => 21],
            ['id' => 22],
            ['id' => 23],
            ['id' => 24],
            ['id' => 25],
            ['id' => 26],
            ['id' => 27],
            ['id' => 28],
            ['id' => 29],
            ['id' => 30],
            ['id' => 31],
            ['id' => 32],
            ['id' => 33],
            ['id' => 34],
            ['id' => 35],
            ['id' => 36],
            ['id' => 37],
            ['id' => 38],
            ['id' => 39],
            ['id' => 40],
            ['id' => 41],
            ['id' => 42],
            ['id' => 43],
            ['id' => 115],
            ['id' => 116],
            ['id' => 117],
            ['id' => 118],
            ['id' => 119],
            ['id' => 120],
            ['id' => 121],
            ['id' => 122],
            ['id' => 139],
            ['id' => 140],
            ['id' => 141],
            ['id' => 142],
            ['id' => 143],
            ['id' => 144],
        ];
        $co = [
            ['id' => 28],
            ['id' => 32],
            ['id' => 36],
            ['id' => 37],
            ['id' => 38],
            ['id' => 40],
            ['id' => 44],
            ['id' => 45],
            ['id' => 46],
            ['id' => 52],
            ['id' => 53],
            ['id' => 54],
            ['id' => 55],
            ['id' => 56],
            ['id' => 57],
            ['id' => 58],
            ['id' => 59],
            ['id' => 60],
            ['id' => 61],
            ['id' => 62],
            ['id' => 63],
            ['id' => 64],
            ['id' => 67],
            ['id' => 68],
            ['id' => 69],
        ];
        
        $per_ceo = Permission::pluck('id', 'id')->all();
        $per_managing_director = Permission::whereNotIn('id',$managing_director)->pluck('id','id');
        $bm = Permission::whereNotIn('id',$bm)->pluck('id','id');
        $co = Permission::whereIn('id',$co)->pluck('id','id');
        $Admin = Permission::pluck('id', 'id')->all();
       
        $role1 = Role::find(1);
        $role2 = Role::find(2);
        $role3 = Role::find(3);
        $role4 = Role::find(4);
        $role5 = Role::find(5);
       
        $role1->syncPermissions($per_ceo);
        $role2->syncPermissions($per_managing_director);
        $role3->syncPermissions($bm);
        $role4->syncPermissions($co);
        $role5->syncPermissions($Admin);
        
        $user1 = User::find(1);
        $user1->assignRole([$role1->id]);
    }
}