<?php

use App\Models\Group;
use App\Models\User;

function createUserWithGroup(string $groupName): User
{
    $group = Group::firstOrCreate(['name' => $groupName]);
    $user = User::factory()->create();
    $user->groups()->attach($group);

    return $user->fresh();
}

test('groups seeder creates admin, members, and developer groups', function () {
    $this->seed(GroupSeeder::class);

    expect(Group::where('name', Group::ADMIN)->exists())->toBeTrue();
    expect(Group::where('name', Group::MEMBERS)->exists())->toBeTrue();
    expect(Group::where('name', Group::DEVELOPER)->exists())->toBeTrue();
});

test('user in admin group is recognized as admin and can manage all', function () {
    $admin = createUserWithGroup(Group::ADMIN);

    expect($admin->isAdmin())->toBeTrue();
    expect($admin->canManageAll())->toBeTrue();
    expect($admin->isMember())->toBeFalse();
});

test('developer is treated as admin for management purposes', function () {
    $developer = createUserWithGroup(Group::DEVELOPER);

    expect($developer->isAdmin())->toBeTrue();
    expect($developer->isDeveloper())->toBeTrue();
    expect($developer->canManageAll())->toBeTrue();
});

test('member is not admin and cannot manage all', function () {
    $member = createUserWithGroup(Group::MEMBERS);

    expect($member->isAdmin())->toBeFalse();
    expect($member->isMember())->toBeTrue();
    expect($member->canManageAll())->toBeFalse();
});

test('inGroup accepts a single group or an array', function () {
    $member = createUserWithGroup(Group::MEMBERS);

    expect($member->inGroup(Group::MEMBERS))->toBeTrue();
    expect($member->inGroup([Group::ADMIN, Group::MEMBERS]))->toBeTrue();
    expect($member->inGroup([Group::ADMIN, Group::DEVELOPER]))->toBeFalse();
});

test('new users start with zero credit', function () {
    $user = User::factory()->create();

    expect($user->credit)->toBe(0);
});
