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

use PHPUnit\Framework\TestCase;
use admin\Controllers\ShopAttributeController;
use Domain\Attribute\AttributeRepository;
use Domain\Languages\LanguagesRepository;

class ShopAttributeControllerTest extends TestCase
{
    private $attributeRepository;
    private $languagesRepository;
    private $controller;

    protected function setUp(): void
    {
        $this->attributeRepository = $this->createMock(AttributeRepository::class);
        $this->languagesRepository = $this->createMock(LanguagesRepository::class);
        $this->controller = new ShopAttributeController(
            $this->attributeRepository,
            $this->languagesRepository
        );
    }

    public function testConstructorAcceptsRepositories(): void
    {
        $controller = new ShopAttributeController(
            $this->attributeRepository,
            $this->languagesRepository
        );

        $this->assertInstanceOf(ShopAttributeController::class, $controller);
    }

    public function testHasMainActionMethods(): void
    {
        $this->assertTrue(method_exists($this->controller, 'list'));
        $this->assertTrue(method_exists($this->controller, 'edit'));
        $this->assertTrue(method_exists($this->controller, 'save'));
        $this->assertTrue(method_exists($this->controller, 'delete'));
        $this->assertTrue(method_exists($this->controller, 'values'));
        $this->assertTrue(method_exists($this->controller, 'values_save'));
        $this->assertTrue(method_exists($this->controller, 'value_row_tpl'));
    }

    public function testHasNoLegacyAliasMethods(): void
    {
        $this->assertFalse(method_exists($this->controller, 'view_list'));
        $this->assertFalse(method_exists($this->controller, 'attribute_edit'));
        $this->assertFalse(method_exists($this->controller, 'attribute_save'));
        $this->assertFalse(method_exists($this->controller, 'attribute_delete'));
        $this->assertFalse(method_exists($this->controller, 'attribute_values'));
    }

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

        $this->assertEquals('string', (string)$reflection->getMethod('list')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('edit')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('delete')->getReturnType());
        $this->assertEquals('string', (string)$reflection->getMethod('values')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('values_save')->getReturnType());
        $this->assertEquals('void', (string)$reflection->getMethod('value_row_tpl')->getReturnType());
    }

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

        $this->assertCount(2, $params);
        $this->assertEquals('Domain\Attribute\AttributeRepository', $params[0]->getType()->getName());
        $this->assertEquals('Domain\Languages\LanguagesRepository', $params[1]->getType()->getName());
    }

    public function testValidateValuesRowsReturnsErrorsForMissingDefaultLanguageAndDefaultSelection(): void
    {
        $reflection = new \ReflectionClass(ShopAttributeController::class);
        $method = $reflection->getMethod('validateValuesRows');
        $method->setAccessible(true);

        $errors = $method->invoke($this->controller, [
            [
                'is_default' => false,
                'translations' => [
                    'pl' => ['name' => ''],
                ],
                'impact_on_the_price' => '10,50',
            ],
            [
                'is_default' => false,
                'translations' => [
                    'pl' => ['name' => 'Rozmiar M'],
                ],
                'impact_on_the_price' => 'abc',
            ],
        ], 'pl');

        $this->assertNotEmpty($errors);
        $this->assertContains('Wiersz nr 1: nazwa w jezyku domyslnym jest wymagana.', $errors);
        $this->assertContains('Wiersz nr 2: nieprawidlowy format "wplyw na cene".', $errors);
        $this->assertContains('Wybierz dokladnie jedna wartosc domyslna.', $errors);
    }

    public function testValidateValuesRowsReturnsEmptyArrayForValidRows(): void
    {
        $reflection = new \ReflectionClass(ShopAttributeController::class);
        $method = $reflection->getMethod('validateValuesRows');
        $method->setAccessible(true);

        $errors = $method->invoke($this->controller, [
            [
                'is_default' => true,
                'translations' => [
                    'pl' => ['name' => 'Rozmiar S'],
                ],
                'impact_on_the_price' => '0.00',
            ],
            [
                'is_default' => false,
                'translations' => [
                    'pl' => ['name' => 'Rozmiar M'],
                ],
                'impact_on_the_price' => '-1,25',
            ],
        ], 'pl');

        $this->assertSame([], $errors);
    }
}