<?php

use App\Http\Helpers\ApiResponse;

test('success returns 200 with status and message', function () {
    $r = ApiResponse::success(['x' => 1], 'OK');
    expect($r->getStatusCode())->toBe(200);
    $body = $r->getData(true);
    expect($body['status'])->toBe('success');
    expect($body['message'])->toBe('OK');
    expect($body['data'])->toBe(['x' => 1]);
});

test('success omits data key when data is null', function () {
    $body = ApiResponse::success(null, 'no body')->getData(true);
    expect($body)->not->toHaveKey('data');
});

test('error returns the given code and message', function () {
    $r = ApiResponse::error('Boom', 418);
    expect($r->getStatusCode())->toBe(418);
    $body = $r->getData(true);
    expect($body['status'])->toBe('error');
    expect($body['message'])->toBe('Boom');
});

test('error includes errors array when provided', function () {
    $body = ApiResponse::error('Bad', 422, ['field' => 'is broken'])->getData(true);
    expect($body['errors'])->toBe(['field' => 'is broken']);
});

test('created returns 201', function () {
    expect(ApiResponse::created(['id' => 5])->getStatusCode())->toBe(201);
});

test('notFound returns 404 with default message', function () {
    $r = ApiResponse::notFound();
    expect($r->getStatusCode())->toBe(404);
    expect($r->getData(true)['message'])->toBe('Resource not found');
});

test('unauthorized returns 401', function () {
    expect(ApiResponse::unauthorized()->getStatusCode())->toBe(401);
});

test('forbidden returns 403', function () {
    expect(ApiResponse::forbidden()->getStatusCode())->toBe(403);
});

test('validationError returns 422 with errors payload', function () {
    $r = ApiResponse::validationError(['email' => ['required']]);
    expect($r->getStatusCode())->toBe(422);
    expect($r->getData(true)['errors'])->toBe(['email' => ['required']]);
});

test('serverError returns 500', function () {
    expect(ApiResponse::serverError()->getStatusCode())->toBe(500);
});
