<?php

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

class RolePermissionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        // Reset cached roles and permissions
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        Permission::create(['name' => 'view-payslips']);
        Permission::create(['name' => 'upload-payslips']);
        Permission::create(['name' => 'edit-payslips']);
        Permission::create(['name' => 'delete-payslips']);
        Permission::create(['name' => 'publish-payslips']);
        Permission::create(['name' => 'unpublish-payslips']);
        Permission::create(['name' => 'create-school']);
        Permission::create(['name' => 'edit-school']);
        Permission::create(['name' => 'delete-school']);
        Permission::create(['name' => 'create-users']);
        Permission::create(['name' => 'edit-users']);
        Permission::create(['name' => 'delete-users']);

        // create roles and assign existing permissions
        $role1 = Role::create(['name' => 'super-admin']);

       /* $role1->givePermissionTo('create school');
        $role1->givePermissionTo('edit school');
        $role1->givePermissionTo('delete school');
        $role1->givePermissionTo('view payslips');
*/
        $role2 = Role::create(['name' => 'admin']);
        $role2->givePermissionTo('view-payslips');
        $role2->givePermissionTo('upload-payslips');
        $role2->givePermissionTo('edit-payslips');
        $role2->givePermissionTo('delete-payslips');
        $role2->givePermissionTo('publish-payslips');
        $role2->givePermissionTo('unpublish-payslips');
        $role2->givePermissionTo('create-users');
        $role2->givePermissionTo('edit-users');
        $role2->givePermissionTo('delete-users');

        $role3 = Role::create(['name' => 'user']);
        $role3->givePermissionTo('view-payslips');

        // create a demo user

        $user = Factory(App\User::class)->create([
             'name' => 'Super Admin User',
             'email' => 'super.admin@example.com',
             'school_id' => 3,
             'ippis_number' => 'TI160033'
             // factory default password is 'password'
         ]);

        $user->assignRole($role1);

        // create a demo user

        $user = Factory(App\User::class)->create([
            'name' => 'Admin User',
            'email' => 'admin@example.com',
            'school_id' => 3,
            'ippis_number' => 'TI75383'
            // factory default password is 'password'
        ]);

        $user->assignRole($role2);

        // create a demo user

        $user = Factory(App\User::class)->create([
            'name' => 'User User',
            'email' => 'user@example.com',
            'school_id' => 3,
            'ippis_number' => 'TI75381'
            // factory default password is 'password'
        ]);

        $user->assignRole($role3);

    }
}
