<?php

namespace Tests\Unit\Framework;

use Openguru\OpenCartFramework\Http\Request;
use Tests\TestCase;

class RequestTest extends TestCase
{
    /** @test */
    public function get_returns_query_parameter(): void
    {
        $request = Request::create('/test?key=value', 'GET');
        
        $this->assertEquals('value', $request->get('key'));
        $this->assertNull($request->get('non_existent'));
        $this->assertEquals('default', $request->get('non_existent', 'default'));
    }

    /** @test */
    public function json_returns_decoded_json_content(): void
    {
        $data = ['key' => 'value', 'nested' => ['foo' => 'bar']];
        $request = Request::create('/test', 'POST', [], [], [], [], json_encode($data));
        
        $this->assertEquals($data, $request->json());
        $this->assertEquals('value', $request->json('key'));
        $this->assertEquals('bar', $request->json('nested.foo'));
        $this->assertNull($request->json('non_existent'));
        $this->assertEquals('default', $request->json('non_existent', 'default'));
    }

    /** @test */
    public function json_returns_default_for_empty_content(): void
    {
        $request = Request::create('/test', 'POST');
        
        $this->assertNull($request->json());
        $this->assertEquals('default', $request->json('key', 'default'));
    }

    /** @test */
    public function get_client_ip_returns_forwarded_for(): void
    {
        $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.1';
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
        
        $request = Request::create('/test');
        
        $this->assertEquals('192.168.1.1', $request->getClientIp());
        
        unset($_SERVER['HTTP_X_FORWARDED_FOR'], $_SERVER['REMOTE_ADDR']);
    }

    /** @test */
    public function get_client_ip_returns_remote_addr_when_no_forwarded_for(): void
    {
        unset($_SERVER['HTTP_X_FORWARDED_FOR']);
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
        
        $request = Request::create('/test');
        
        $this->assertEquals('127.0.0.1', $request->getClientIp());
        
        unset($_SERVER['REMOTE_ADDR']);
    }

    /** @test */
    public function get_user_agent_returns_user_agent(): void
    {
        $_SERVER['HTTP_USER_AGENT'] = 'Test User Agent';
        
        $request = Request::create('/test');
        
        $this->assertEquals('Test User Agent', $request->getUserAgent());
        
        unset($_SERVER['HTTP_USER_AGENT']);
    }

    /** @test */
    public function header_returns_header_value(): void
    {
        $request = Request::create('/test', 'GET', [], [], [], [
            'HTTP_X_CUSTOM_HEADER' => 'custom-value'
        ]);
        
        $this->assertEquals('custom-value', $request->header('X-Custom-Header'));
        $this->assertNull($request->header('Non-Existent'));
        $this->assertEquals('default', $request->header('Non-Existent', 'default'));
    }

    /** @test */
    public function get_api_key_returns_api_key_from_header(): void
    {
        $_SERVER['HTTP_X_API_KEY'] = 'test-api-key';
        
        $request = Request::create('/test');
        
        $this->assertEquals('test-api-key', $request->getApiKey());
        
        unset($_SERVER['HTTP_X_API_KEY']);
    }

    /** @test */
    public function get_api_key_returns_null_when_not_set(): void
    {
        unset($_SERVER['HTTP_X_API_KEY']);
        
        $request = Request::create('/test');
        
        $this->assertNull($request->getApiKey());
    }
}
