<?php

use App\Http\Resources\TeamMemberResource;
use App\Models\Role;
use App\Models\TeamMember;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

test('team member resource wraps data and includes role when loaded', function () {
    $role = Role::factory()->create();
    $teamMember = TeamMember::factory()->create(['role_id' => $role->id]);
    $teamMember->load('role');

    $response = (new TeamMemberResource($teamMember))->toResponse(Request::create('/'));
    $payload = $response->getData(true);

    expect($payload['data']['id'])->toBe($teamMember->id);
    expect($payload['data']['role']['id'])->toBe($role->id);
});

test('team member resource includes scalar role_id', function () {
    $role = Role::factory()->create();
    $teamMember = TeamMember::factory()->create(['role_id' => $role->id]);
    $teamMember->load('role');

    $response = (new TeamMemberResource($teamMember))->toResponse(Request::create('/'));
    $payload = $response->getData(true);

    expect($payload['data'])->toHaveKey('role_id');
    expect($payload['data']['role_id'])->toBe($teamMember->role_id);
});

test('team member resource collection keeps data wrapper', function () {
    $role = Role::factory()->create();
    $teamMembers = TeamMember::factory()->count(2)->create(['role_id' => $role->id]);

    $response = TeamMemberResource::collection($teamMembers)->toResponse(Request::create('/'));
    $payload = $response->getData(true);

    expect($payload['data'])->toHaveCount(2);
});
