<?php

declare(strict_types=1);

use Builder\Core\Models\Role;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

beforeEach(function (): void {
    Schema::create('roles', function (Blueprint $table): void {
        $table->id();
        $table->string('name');
        $table->string('display_name')->nullable();
        $table->string('description')->nullable();
        $table->timestamps();
    });

    Schema::create('users', function (Blueprint $table): void {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('role_user', function (Blueprint $table): void {
        $table->foreignId('role_id');
        $table->foreignId('user_id');
        $table->primary(['role_id', 'user_id']);
    });
});

it('Role uses the roles table', function (): void {
    $role = Role::create(['name' => 'editor']);

    expect(Role::find($role->id)?->name)->toBe('editor');
});

it('Role users() is a BelongsToMany relation', function (): void {
    $role = Role::create(['name' => 'admin']);

    expect($role->users())->toBeInstanceOf(BelongsToMany::class);
});
