<?php

use App\User;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateManagerUserRole extends Migration
{
    public function up()
    {
        // Admin role ID was changed from 99 to 100.
        User::where('role', '=', 99)->update(['role' => 100]);

        Schema::table('users', function (Blueprint $table) {
            $table
                ->boolean('is_manager')
                ->default(false)
                ->nullable(false);
        });
    }

    public function down()
    {
        // Remove role from managers to avoid implicitly promoting them to administrators.
        User::where('role', '=', 99)->update(['role' => 0]);

        // Reset admin role ID.
        User::where('role', '=', 100)->update(['role' => 99]);

        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('is_manager');
        });
    }
}
