<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();
            $table->string('name');
            $table->timestamps();
        });

        DB::table('roles')->insert([
            ['slug' => 'super_admin', 'name' => 'Super administrador', 'created_at' => now(), 'updated_at' => now()],
            ['slug' => 'administrador', 'name' => 'Administrador', 'created_at' => now(), 'updated_at' => now()],
            ['slug' => 'organizador', 'name' => 'Organizador', 'created_at' => now(), 'updated_at' => now()],
            ['slug' => 'cliente', 'name' => 'Cliente', 'created_at' => now(), 'updated_at' => now()],
        ]);

        Schema::table('users', function (Blueprint $table) {
            $table->foreignId('role_id')
                ->after('password')
                ->default(4)
                ->constrained()
                ->restrictOnDelete();
        });
    }

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

        Schema::dropIfExists('roles');
    }
};
