<?php

use App\Http\Resources\ProjectResource;
use App\Models\Project;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Tests\TestCase;

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

test('project resource includes expected fields inside data wrapper', function () {
    $project = Project::factory()->approved()->create();
    $project->load(['status', 'type']);

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

    expect($payload)->toHaveKey('data');
    expect($payload['data'])->toHaveKey('status');
    expect($payload['data'])->toHaveKey('type');
    expect($payload['data'])->toHaveKey('approved_estimate');
});

test('project resource includes scalar type_id and status_id', function () {
    $project = Project::factory()->approved()->create();
    $project->load(['status', 'type']);

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

    expect($payload['data'])->toHaveKey('type_id');
    expect($payload['data'])->toHaveKey('status_id');
    expect($payload['data']['type_id'])->toBe($project->type_id);
    expect($payload['data']['status_id'])->toBe($project->status_id);
});

test('project resource collection wraps multiple entries', function () {
    $projects = Project::factory()->count(2)->create();

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

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