<?php

namespace Tests\Unit\Framework;

use InvalidArgumentException;
use Openguru\OpenCartFramework\Support\Utils;
use PHPUnit\Framework\TestCase;

class UtilsTest extends TestCase
{
    public function testUcsplitSplitsStringByUppercase(): void
    {
        $result = Utils::ucsplit('HelloWorld');
        $this->assertEquals(['Hello', 'World'], $result);
    }

    public function testUcsplitWithMultipleWords(): void
    {
        $result = Utils::ucsplit('HelloWorldTest');
        $this->assertEquals(['Hello', 'World', 'Test'], $result);
    }

    public function testUcsplitWithSingleWord(): void
    {
        $result = Utils::ucsplit('Hello');
        $this->assertEquals(['Hello'], $result);
    }

    public function testUcsplitWithEmptyString(): void
    {
        $result = Utils::ucsplit('');
        $this->assertEquals([], $result);
    }

    public function testSafeJsonDecodeWithValidJson(): void
    {
        $json = '{"key": "value", "number": 123}';
        $result = Utils::safeJsonDecode($json);
        
        $this->assertIsArray($result);
        $this->assertEquals('value', $result['key']);
        $this->assertEquals(123, $result['number']);
    }

    public function testSafeJsonDecodeWithArray(): void
    {
        $json = '[1, 2, 3]';
        $result = Utils::safeJsonDecode($json);
        
        $this->assertIsArray($result);
        $this->assertEquals([1, 2, 3], $result);
    }

    public function testSafeJsonDecodeThrowsExceptionOnInvalidJson(): void
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('JSON error:');
        
        Utils::safeJsonDecode('invalid json {');
    }

    public function testArrayFlattenWithNestedArrays(): void
    {
        $array = [1, [2, 3], [4, [5, 6]]];
        $result = Utils::arrayFlatten($array);
        
        $this->assertEquals([1, 2, 3, 4, 5, 6], $result);
    }

    public function testArrayFlattenWithDepthLimit(): void
    {
        $array = [1, [2, [3, 4]]];
        $result = Utils::arrayFlatten($array, 1);
        
        $this->assertEquals([1, 2, [3, 4]], $result);
    }

    public function testArrayFlattenWithFlatArray(): void
    {
        $array = [1, 2, 3];
        $result = Utils::arrayFlatten($array);
        
        $this->assertEquals([1, 2, 3], $result);
    }

    public function testArrayFlattenWithEmptyArray(): void
    {
        $result = Utils::arrayFlatten([]);
        
        $this->assertEquals([], $result);
    }

    public function testStrContainsWithExistingNeedle(): void
    {
        $this->assertTrue(Utils::strContains('hello world', 'world'));
    }

    public function testStrContainsWithNonExistingNeedle(): void
    {
        $this->assertFalse(Utils::strContains('hello world', 'foo'));
    }

    public function testStrContainsWithEmptyNeedle(): void
    {
        $this->assertTrue(Utils::strContains('hello world', ''));
    }

    public function testStrContainsCaseSensitive(): void
    {
        $this->assertTrue(Utils::strContains('Hello World', 'Hello'));
        $this->assertFalse(Utils::strContains('Hello World', 'hello'));
    }
}

