<?php

use App\Http\Resources\PtoResource;
use App\Models\Pto;
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('pto resource returns wrapped data with team member', function () {
    $role = Role::factory()->create();
    $teamMember = TeamMember::factory()->create(['role_id' => $role->id]);

    $pto = Pto::create([
        'team_member_id' => $teamMember->id,
        'start_date' => '2026-02-10',
        'end_date' => '2026-02-12',
        'reason' => 'Travel',
        'status' => 'approved',
    ]);
    $pto->load('teamMember');

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

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

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

    Pto::create([
        'team_member_id' => $member->id,
        'start_date' => '2026-02-10',
        'end_date' => '2026-02-10',
        'reason' => 'Travel',
        'status' => 'approved',
    ]);
    Pto::create([
        'team_member_id' => $member->id,
        'start_date' => '2026-03-10',
        'end_date' => '2026-03-12',
        'reason' => 'Rest',
        'status' => 'approved',
    ]);

    $response = PtoResource::collection(Pto::limit(2)->get())->toResponse(Request::create('/'));
    $payload = $response->getData(true);

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