<?php

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

return new class extends Migration
{
    public function up()
    {
        // 1. Buat tabel roles
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique(); 
            $table->string('description')->nullable();
            $table->timestamps();
        });

        // 2. Modifikasi tabel users
        Schema::table('users', function (Blueprint $table) {
            $table->foreignId('role_id')
                  ->nullable() 
                  ->after('id')
                  ->constrained('roles')
                  ->onDelete('set null');
            
            // Kolom ini akan menyimpan 'slug' kampus yang dikelola Admin Kampus
            $table->string('campus_slug')->nullable()->after('role_id'); 
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['role_id']);
            $table->dropColumn('role_id');
            $table->dropColumn('campus_slug');
        });
        
        Schema::dropIfExists('roles');
    }
};