<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;


class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        DB::table('permissions')->truncate();
        DB::table('model_has_permissions')->truncate();
        DB::table('role_has_permissions')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        $permissions_map = ['l' => 'list', 'c' => 'create', 'r' => 'show', 'u' => 'edit', 'd' => 'delete'];

        // Bank Account
        $resourece_features['bank_account'] = ['l', 'c','r','d'];

        // Expense
        $resourece_features['expense_category'] = ['l', 'c', 'u', 'd'];
        $resourece_features['expense_sub_category'] = ['l', 'c', 'u', 'd'];
        $resourece_features['expense'] = ['l', 'c', 'u', 'd'];

        // Expense
        // $resourece_features['income_sub_category'] = ['l', 'c', 'u', 'd'];
        // $resourece_features['income_category'] = ['l', 'c', 'u', 'd'];
        // $resourece_features['income'] = ['l', 'c', 'u', 'd'];

        // Loan
        $resourece_features['loan'] = ['l', 'c', 'r', 'u', 'd'];
        $other_permissions['withdraw-loan-savings'] = 'loan';

        // Savings
        $resourece_features['savings'] = ['l', 'c', 'r', 'u', 'd'];
        $other_permissions['withdraw-saving']  = 'savings';


        // DPS
        $resourece_features['dps'] = ['l', 'c', 'r', 'u', 'd'];
        $other_permissions['dps_status']  = 'dps';
        // Customer
        $resourece_features['customer'] = ['l', 'c', 'r', 'u', 'd'];
        $other_permissions['customer-profile']='customer';

       // Loan & DPS Reports
        $other_permissions['loan_report'] = 'report';
        $other_permissions['dps_report']  = 'report';
        $other_permissions['collection_report']  = 'report';

        // Installment / Payments
        $other_permissions['loan_payment'] = 'collection';
        $other_permissions['dps_payment']  = 'collection';
        $other_permissions['transaction']  = 'transaction';


        // Setting
        $other_permissions['setting']='misc';
        $other_permissions['backup']='misc';
        // $other_permissions['roles']='roles';

        $resourece_features['role'] = ['l', 'c', 'u', 'd'];
        $resourece_features['user'] = ['l', 'c', 'u', 'd'];
        $other_permissions['permissions']='role';


        $other_permissions['profile']='profile';
        $other_permissions['change_password']='profile';

        // Dashboard
        $other_permissions['dashboard']='dashboard';
        $other_permissions['today_expense']='dashboard';
        $other_permissions['today_dps']='dashboard';
        $other_permissions['today_loan']='dashboard';
        $other_permissions['today_balance']='dashboard';

        $other_permissions['monthly_expense']='dashboard';
        $other_permissions['monthly_loan']='dashboard';
        $other_permissions['monthly_Balance']='dashboard';
        $other_permissions['monthly_dps']='dashboard';

        $other_permissions['total_expense']='dashboard';
        $other_permissions['total_loan']='dashboard';
        $other_permissions['total_dps']='dashboard';
        $other_permissions['total_Balance']='dashboard';
        $other_permissions['total_loan_due']='dashboard';
        $other_permissions['total_loan_paid']='dashboard';
        $other_permissions['total_loan_amount']='dashboard';
        $other_permissions['total_dps_due']='dashboard';
        $other_permissions['total_dps_paid']='dashboard';
        $other_permissions['total_dps_amount']='dashboard';
        $other_permissions['total_profit']='dashboard';
        $other_permissions['total_user']='dashboard';
        $other_permissions['total_customer']='dashboard';


        foreach ($resourece_features as $key => $rf) {
            foreach ($rf as $feature) {
                $access = $permissions_map[$feature];
                Permission::create([
                    'name' => $access . "-" . $key,
                    'feature' => $key
                ]);
            }
        }

        foreach ($other_permissions as $permission => $value) {
            Permission::create([
                'name' => $permission,
                'feature' => $value
            ]);
        }

        $all_permissions = Permission::pluck('name');
        $admin = Role::where('name','admin')->first();
        $test_admin = Role::where('name','test_admin')->first();

        $admin->syncPermissions($all_permissions);
        $test_admin->syncPermissions($all_permissions);

        $operator = Role::where('name','operator')->first();

        $operator_permissions = [
            'profile',
            'change_password'
        ];

        $operator->syncPermissions($operator_permissions);
    }
}
