<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class PermissionSeeder extends Seeder{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(){
        // Rest cached roles and permissions
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        $permissions = array_merge(config("admin-permissions"),config("stocks-management.permissions"));

        //dd($permissions);
        $guard = "admin";
        foreach ($permissions as $permission) {

            $permission['guard_name'] = $guard;
            Permission::updateOrCreate(
                [
                    "name" => $permission["name"],
                    "guard_name" => $guard,
                ],
                $permission
            );

//            if (isset($permission['guard_name'])) {
//                $guard = $permission["guard_name"];
//                Permission::updateOrCreate(
//                    [
//                        "name" => $permission["name"],
//                        "guard_name" => $permission["guard_name"],
//                    ],
//                    $permission
//                );
//                echo(__("[$guard] :permission OK!\n",['permission' => $permission['name']]));
//
//            }
//            else{
//                $guards = array_keys(Arr::except(config("auth.guards"),['api']));
//
//                foreach ($guards as $guard){
//                    Permission::updateOrCreate(
//                        [
//                            "name" => $permission["name"],
//                            "guard_name" => $guard,
//                        ],
//                        $permission
//                    );
//
//                    echo(__("[$guard] :permission OK!\n",['permission' => $permission['name']]));
//
//                }
//            }
        }

       // $permissions = config("stocks-management.permissions");

//        if ($permissions){
//            echo(__("[GESTION DE STOCK] Detection de :count permission(s)\n",['count' => format_amount(count($permissions))]));
//
//            $guard = "admin";
//          //  $guard = $permission["guard_name"] ?? "web";
//            foreach ($permissions as $permission) {
//
//                Permission::updateOrCreate(
//                    [
//                        "name" => $permission["name"],
//                        "guard_name" => $permission["guard_name"],
//                    ],
//                    $permission
//                );
//                echo(__("[$guard] :permission OK!\n",['permission' => $permission['name']]));
//
//            }
//        }
    }
}

