<?php
namespace Database\Seeders;

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

class RoleSeeder extends Seeder
{
  /**
   * Run the database seeds.
   *
   * @return void
   */
  public function run()
  {
    $user_admin=Role::updateOrCreate(['name'=>'User Admin', 'description'=>'Administrates Users']);
    $role_admin=Role::updateOrCreate(['name'=>'Role Admin', 'description'=>'Administrates Roles']);


    $user_perms=['Create User','Edit User','Delete User','Reset Password','Assign Role'];
    $uperms=array();
    foreach ($user_perms as $perm){
      array_push($uperms,Permission::where('name',$perm)->first()->id);
    }
    $user_admin->permissions()->sync($uperms);

    $role_perms=['Create Role','Edit Role','Delete Role'];
    $rperms=array();
    foreach ($role_perms as $perm){
      array_push($rperms,Permission::where('name',$perm)->first()->id);
    }
    $role_admin->permissions()->sync($rperms);

    //Add User and Role Admin to first entry in Users Table
    $first_user = User::orderBy('id')->first();
    $first_user->roles()->sync([$user_admin->id,$role_admin->id]);
  }
}
