<?php
declare(strict_types=1);

namespace Tests\Unit\Api;

use Tests\TestCase;
use App\Core\Response;

final class ResponseTest extends TestCase
{
    public function testWithHeadersSetsMultipleHeaders(): void
    {
        $response = new Response();
        $response->json(['ok' => true]);
        $response->withHeaders([
            'X-Custom-One' => 'value1',
            'X-Custom-Two' => 'value2',
        ]);

        $ref = new \ReflectionClass($response);
        $headersProp = $ref->getProperty('headers');
        $headersProp->setAccessible(true);
        $headers = $headersProp->getValue($response);

        $this->assertEquals('value1', $headers['X-Custom-One']);
        $this->assertEquals('value2', $headers['X-Custom-Two']);
    }

    public function testHeaderMethodChaining(): void
    {
        $response = new Response();
        $result = $response->header('X-Test', 'hello')->header('X-Other', 'world');

        $this->assertSame($response, $result);

        $ref = new \ReflectionClass($response);
        $headersProp = $ref->getProperty('headers');
        $headersProp->setAccessible(true);
        $headers = $headersProp->getValue($response);

        $this->assertEquals('hello', $headers['X-Test']);
        $this->assertEquals('world', $headers['X-Other']);
    }

    public function testJsonSetsContentType(): void
    {
        $response = new Response();
        $response->json(['success' => true], 201);

        $ref = new \ReflectionClass($response);
        $headersProp = $ref->getProperty('headers');
        $headersProp->setAccessible(true);
        $statusProp = $ref->getProperty('statusCode');
        $statusProp->setAccessible(true);

        $headers = $headersProp->getValue($response);
        $this->assertEquals('application/json; charset=UTF-8', $headers['Content-Type']);
        $this->assertEquals(201, $statusProp->getValue($response));
    }

    public function testStatusMethodSetsCode(): void
    {
        $response = new Response();
        $response->status(404);

        $ref = new \ReflectionClass($response);
        $statusProp = $ref->getProperty('statusCode');
        $statusProp->setAccessible(true);

        $this->assertEquals(404, $statusProp->getValue($response));
    }
}
