<?php

use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\DB;

function getMigrationInstance(): object
{
    return require database_path('migrations/2026_03_14_000001_rename_member_to_worker_in_workspace_user.php');
}

test('data migration renames member to worker in workspace_user role', function () {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create();

    // Insert a record with the old 'member' value directly via DB
    DB::table('workspace_user')->insert([
        'workspace_id' => $workspace->id,
        'user_id' => $user->id,
        'role' => 'member',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    // Verify it has 'member' role
    expect(
        DB::table('workspace_user')
            ->where('user_id', $user->id)
            ->where('workspace_id', $workspace->id)
            ->value('role')
    )->toBe('member');

    // Run the actual migration file
    getMigrationInstance()->up();

    // Verify it now has 'worker' role
    expect(
        DB::table('workspace_user')
            ->where('user_id', $user->id)
            ->where('workspace_id', $workspace->id)
            ->value('role')
    )->toBe('worker');
});

test('data migration does not affect owner or manager roles', function () {
    $owner = User::factory()->create();
    $manager = User::factory()->create();
    $worker = User::factory()->create();
    $workspace = Workspace::factory()->create();

    DB::table('workspace_user')->insert([
        ['workspace_id' => $workspace->id, 'user_id' => $owner->id, 'role' => 'owner', 'created_at' => now(), 'updated_at' => now()],
        ['workspace_id' => $workspace->id, 'user_id' => $manager->id, 'role' => 'manager', 'created_at' => now(), 'updated_at' => now()],
        ['workspace_id' => $workspace->id, 'user_id' => $worker->id, 'role' => 'member', 'created_at' => now(), 'updated_at' => now()],
    ]);

    // Run the actual migration file
    getMigrationInstance()->up();

    // Owner and manager should remain unchanged
    expect(DB::table('workspace_user')->where('user_id', $owner->id)->value('role'))->toBe('owner');
    expect(DB::table('workspace_user')->where('user_id', $manager->id)->value('role'))->toBe('manager');
    expect(DB::table('workspace_user')->where('user_id', $worker->id)->value('role'))->toBe('worker');
});
