<?php

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

/**
 * Class RolesSeeder
 */
class RolesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        foreach (Permission::ALL_PERMISSIONS as $id => $permission) {
            (new Permission([
                'id' => $id,
                'name' => $permission
            ]))->save();
        }

        $permissionTransitionsTable = DB::table('state_transitions');
        foreach (Permission::ALL_STATE_TRANSITIONS as $id => $permission) {

            $fromStateId = $toStateId = null;

            switch ($permission) {
                case Permission::STATE_TRANSITION_INACTIVE_ACTIVE:
                    $fromStateId = \App\State::getStateId(\App\State::INACTIVE);
                    $toStateId = \App\State::getStateId(\App\State::ACTIVE);
                    break;
                case Permission::STATE_TRANSITION_ACTIVE_INACTIVE:
                    $fromStateId = \App\State::getStateId(\App\State::ACTIVE);
                    $toStateId = \App\State::getStateId(\App\State::INACTIVE);
                    break;
                case Permission::STATE_TRANSITION_ACTIVE_REPAIRING:
                    $fromStateId = \App\State::getStateId(\App\State::ACTIVE);
                    $toStateId = \App\State::getStateId(\App\State::REPAIRING);
                    break;
                case Permission::STATE_TRANSITION_REPAIRING_ACTIVE:
                    $fromStateId = \App\State::getStateId(\App\State::REPAIRING);
                    $toStateId = \App\State::getStateId(\App\State::ACTIVE);
                    break;
                case Permission::STATE_TRANSITION_ACTIVE_WRITTEN_OFF:
                    $fromStateId = \App\State::getStateId(\App\State::ACTIVE);
                    $toStateId = \App\State::getStateId(\App\State::WRITTEN_OFF);
                    break;
            }

            $permissionTransitionsTable->insert([
                'from_state_id' => $fromStateId,
                'to_state_id' => $toStateId,
                'permission_id' => $id
            ]);
        }

        foreach (Role::ALL_ROLES as $id => $roleName) {

            $role = new Role([
                'name' => $roleName
            ]);

            $role->save();

            switch ($roleName) {

                case Role::ADMIN:
                    $role->attachPermissions(
                        array_diff(array_keys(Permission::ALL_PERMISSIONS), [
                            // Before proceeding to allow create / edit / delete Entity Classes, see this:
                            // https://bootsnipp.com/snippets/AXVrV
                            Permission::CREATE_DEVICE_CLASS_ID,
                            Permission::EDIT_DEVICE_CLASS_ID,
                            Permission::DELETE_DEVICE_CLASS_ID
                        ])
                    );
                    break;

                case Role::DEVICE_MANAGER:
                    $role->attachPermissions(
                        array_keys(Permission::DEVICE_MANAGER_PERMISSIONS)
                    );
                    break;

                case Role::AUDITOR:
                    $role->attachPermissions(
                        array_keys(Permission::AUDITOR_PERMISSIONS)
                    );
                    break;

                default:
                    break;
            }
        }
    }
}
