<?php
namespace Tests\Unit\admin\Controllers;

use PHPUnit\Framework\TestCase;
use admin\Controllers\ShopOrderController;
use Domain\Order\OrderAdminService;

class ShopOrderControllerTest extends TestCase
{
    private $service;
    private $controller;

    protected function setUp(): void
    {
        $this->service = $this->createMock(OrderAdminService::class);
        $this->controller = new ShopOrderController($this->service);
    }

    public function testConstructorAcceptsService(): void
    {
        $controller = new ShopOrderController($this->service);
        $this->assertInstanceOf(ShopOrderController::class, $controller);
    }

    public function testHasExpectedActionMethods(): void
    {
        $this->assertTrue(method_exists($this->controller, 'list'));
        $this->assertTrue(method_exists($this->controller, 'view_list'));
        $this->assertTrue(method_exists($this->controller, 'details'));
        $this->assertTrue(method_exists($this->controller, 'order_details'));
        $this->assertTrue(method_exists($this->controller, 'edit'));
        $this->assertTrue(method_exists($this->controller, 'order_edit'));
        $this->assertTrue(method_exists($this->controller, 'save'));
        $this->assertTrue(method_exists($this->controller, 'order_save'));
        $this->assertTrue(method_exists($this->controller, 'notes_save'));
        $this->assertTrue(method_exists($this->controller, 'order_status_change'));
        $this->assertTrue(method_exists($this->controller, 'order_resend_confirmation_email'));
        $this->assertTrue(method_exists($this->controller, 'set_order_as_unpaid'));
        $this->assertTrue(method_exists($this->controller, 'set_order_as_paid'));
        $this->assertTrue(method_exists($this->controller, 'send_order_to_apilo'));
        $this->assertTrue(method_exists($this->controller, 'toggle_trustmate_send'));
        $this->assertTrue(method_exists($this->controller, 'delete'));
        $this->assertTrue(method_exists($this->controller, 'order_delete'));
        $this->assertTrue(method_exists($this->controller, 'search_products_ajax'));
    }

    public function testViewActionsReturnString(): void
    {
        $reflection = new \ReflectionClass($this->controller);

        $this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('view_list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('details')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('order_details')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('edit')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('order_edit')->getReturnType());
    }

    public function testMutationActionsReturnVoid(): void
    {
        $reflection = new \ReflectionClass($this->controller);

        $this->assertEquals('void', (string)$reflection->getMethod('save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('order_save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('notes_save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('order_status_change')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('order_resend_confirmation_email')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('set_order_as_unpaid')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('set_order_as_paid')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('send_order_to_apilo')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('toggle_trustmate_send')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('delete')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('order_delete')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('search_products_ajax')->getReturnType());
    }

    public function testConstructorRequiresOrderAdminService(): void
    {
        $reflection = new \ReflectionClass(ShopOrderController::class);
        $constructor = $reflection->getConstructor();
        $params = $constructor->getParameters();

        $this->assertCount(2, $params);
        $this->assertEquals('Domain\\Order\\OrderAdminService', $params[0]->getType()->getName());
        $this->assertEquals('Domain\\Product\\ProductRepository', $params[1]->getType()->getName());
        $this->assertTrue($params[1]->isOptional());
    }

    // --- contrastTextColor tests (via reflection) ---

    public function testContrastTextColorReturnsBlackForLightColor(): void
    {
        $result = $this->invokePrivate('contrastTextColor', ['#ffffff']);
        $this->assertSame('#000', $result);
    }

    public function testContrastTextColorReturnsWhiteForDarkColor(): void
    {
        $result = $this->invokePrivate('contrastTextColor', ['#000000']);
        $this->assertSame('#fff', $result);
    }

    public function testContrastTextColorHandlesShortHex(): void
    {
        $result = $this->invokePrivate('contrastTextColor', ['#fff']);
        $this->assertSame('#000', $result);

        $result = $this->invokePrivate('contrastTextColor', ['#000']);
        $this->assertSame('#fff', $result);
    }

    public function testContrastTextColorDefaultsToWhiteForInvalidHex(): void
    {
        $result = $this->invokePrivate('contrastTextColor', ['invalid']);
        $this->assertSame('#fff', $result);

        $result = $this->invokePrivate('contrastTextColor', ['#zz']);
        $this->assertSame('#fff', $result);
    }

    // --- sanitizeInlineHtml tests (via reflection) ---

    public function testSanitizeInlineHtmlStripsDisallowedTags(): void
    {
        $result = $this->invokePrivate('sanitizeInlineHtml', ['<b>Bold</b> <script>alert(1)</script> <em>Italic</em>']);
        $this->assertSame('<b>Bold</b> alert(1) <em>Italic</em>', $result);
    }

    public function testSanitizeInlineHtmlStripsAttributesFromAllowedTags(): void
    {
        $result = $this->invokePrivate('sanitizeInlineHtml', ['<b onclick="alert(1)">Bold</b>']);
        $this->assertSame('<b>Bold</b>', $result);

        $result = $this->invokePrivate('sanitizeInlineHtml', ['<strong style="color:red" class="x">text</strong>']);
        $this->assertSame('<strong>text</strong>', $result);
    }

    public function testSanitizeInlineHtmlPreservesCleanTags(): void
    {
        $result = $this->invokePrivate('sanitizeInlineHtml', ['<b>Bold</b> <i>Italic</i> <strong>Strong</strong> <em>Em</em>']);
        $this->assertSame('<b>Bold</b> <i>Italic</i> <strong>Strong</strong> <em>Em</em>', $result);
    }

    public function testSanitizeInlineHtmlHandlesPlainText(): void
    {
        $result = $this->invokePrivate('sanitizeInlineHtml', ['Kurier DPD']);
        $this->assertSame('Kurier DPD', $result);
    }

    private function invokePrivate(string $method, array $args)
    {
        $reflection = new \ReflectionMethod($this->controller, $method);
        $reflection->setAccessible(true);
        return $reflection->invokeArgs($this->controller, $args);
    }
}
