<?php

namespace Database\Seeders;

use App\Models\SecurityManagement\Permission;
use App\Models\SecurityManagement\Role;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class RoleSeeder extends Seeder{

    public function run(){

        $roles = [
            [
                "name" => Role::SUPER_ADMIN,
                "guard_name" => "admin",
                "editable" => false,
                "permissions" => Permission::where("guard_name","admin")
                                            ->get()
                                            ->pluck("id")
                                            ->toArray()
            ],
            // [
            //     "name" => "Supervivor",
            //     "editable" => true,
            //     "guard_name" => "admin",
            // ],
            // [
            //     "name" => "user",
            //     "guard_name" => "web",
            //     "editable" => false,
            //     "permissions" => [
            //         'review-create', 'review-edit', 'review-show', 'review-delete',
            //     ]
            // ],

//            [
//                "name" => Role::CUSTOMER,
//                "guard_name" => "web",
//                "editable" => false,
//                "permissions" => []
//            ],

        ];
       // $supersAdmin = Resource::whereIn('email',['joelsimooverride@gmail.com',"nananguebong@gmail.com"])->get();

        foreach ($roles as $role) {
            $permissions = $role['permissions'] ?? [];
            $name =  $role['name'] ;
            $guard =  $role['guard_name'] ;

            unset($role['permissions']);

            $role =  Role::updateOrCreate(
                    [
                        "name" => $name,
                        "guard_name" =>  $guard,
                    ],
                   $role
                );


           // echo __("[$guard] Role : :name => Ok!\n",['name' => $role->name]);

            if ($permissions and $role instanceof Role){
                $role->syncPermissions($permissions);
              //  echo(__(":count permission(s) synchronisée(s)\n",['count' => $role->permissions->count()]));
            }

            echo __("All Roles & Permissions Created. \n\n");
        }
    }
}
