<?php
/**
 * Copyright 2025 Adobe
 * All Rights Reserved.
 */
declare(strict_types=1);

namespace Magento\Ui\Test\Unit\Component\Filters\Type;

use Magento\Framework\Api\Filter;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
use Magento\Framework\View\Element\UiComponent\Processor;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Ui\Component\Filters\FilterModifier;
use Magento\Ui\Component\Filters\Type\Input;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class InputTest extends TestCase
{
    /**
     * @var ContextInterface|MockObject
     */
    protected $contextMock;

    /**
     * @var UiComponentFactory|MockObject
     */
    protected $uiComponentFactory;

    /**
     * @var FilterBuilder|MockObject
     */
    protected $filterBuilderMock;

    /**
     * @var FilterModifier|MockObject
     */
    protected $filterModifierMock;

    /**
     * @inheritdoc
     */
    protected function setUp(): void
    {
        $this->contextMock = $this->createMock(ContextInterface::class);
        $this->uiComponentFactory = $this->createPartialMock(
            UiComponentFactory::class,
            ['create']
        );
        $this->filterBuilderMock = $this->createMock(FilterBuilder::class);
        $this->filterModifierMock = $this->createPartialMock(
            FilterModifier::class,
            ['applyFilterModifier']
        );
    }

    /**
     * Run test getComponentName method
     *
     * @return void
     */
    public function testGetComponentName(): void
    {
        $this->contextMock->expects($this->never())->method('getProcessor');
        $date = new Input(
            $this->contextMock,
            $this->uiComponentFactory,
            $this->filterBuilderMock,
            $this->filterModifierMock,
            []
        );

        $this->assertSame(Input::NAME, $date->getComponentName());
    }

    /**
     * Run test prepare method
     *
     * @param array $data
     * @param array $filterData
     * @param array|null $expectedCondition
     */
    #[DataProvider('getPrepareDataProvider')]
    public function testPrepare(array $data, array $filterData, ?array $expectedCondition): void
    {
        $processor = $this->createMock(Processor::class);
        $this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processor);
        /** @var UiComponentInterface $uiComponent */
        $uiComponent = $this->createMock(UiComponentInterface::class);

        $uiComponent->expects($this->any())
            ->method('getContext')
            ->willReturn($this->contextMock);

        $this->contextMock->expects($this->any())
            ->method('getNamespace')
            ->willReturn(Input::NAME);
        $this->contextMock->expects($this->any())
            ->method('addComponentDefinition')
            ->with(Input::NAME, ['extends' => Input::NAME]);
        $this->contextMock->expects($this->any())
            ->method('getFiltersParams')
            ->willReturn($filterData);
        $dataProvider = $this->createMock(DataProviderInterface::class);

        $this->contextMock->expects($this->any())
            ->method('getDataProvider')
            ->willReturn($dataProvider);

        $this->uiComponentFactory->expects($this->any())
            ->method('create')
            ->with($data['name'], Input::COMPONENT, ['context' => $this->contextMock])
            ->willReturn($uiComponent);

        if ($expectedCondition !== null) {
            $this->filterBuilderMock->expects($this->once())
                ->method('setConditionType')
                ->with($expectedCondition['setConditionType'])
                ->willReturnSelf();

            $this->filterBuilderMock->expects($this->once())
                ->method('setField')
                ->with($data['name'])
                ->willReturnSelf();

            $this->filterBuilderMock->expects($this->once())
                ->method('setValue')
                ->with($expectedCondition['setValue'])
                ->willReturnSelf();

            $filterMock = $this->createMock(Filter::class);

            $this->filterBuilderMock->expects($this->once())
                ->method('create')
                ->willReturn($filterMock);
        }

        $date = new Input(
            $this->contextMock,
            $this->uiComponentFactory,
            $this->filterBuilderMock,
            $this->filterModifierMock,
            [],
            $data
        );

        $date->prepare();
    }

    /**
     * SuppressWarnings was added due to the big size of data provider
     *
     * @return array
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public static function getPrepareDataProvider(): array
    {
        return [
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => ''],
                null,
            ],
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => null],
                null,
            ],
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => '0'],
                [
                    'setConditionType' => 'like',
                    'setValue' => '%0%',
                ],
            ],
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => 'some_value'],
                [
                    'setConditionType' => 'like',
                    'setValue' => '%some\_value%',
                ],
            ],
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => '%'],
                [
                    'setConditionType' => 'like',
                    'setValue' => '%\%%',
                ],
            ],
            [
                [
                    'name' => 'test_date',
                ],
                ['test_date' => 'some\\value'],
                [
                    'setConditionType' => 'like',
                    'setValue' => '%some\\\\value%',
                ],
            ],
            [
                [
                    'name' => 'text_attr',
                    'config' => [
                        'filter' => [
                            'filterType' => 'text',
                            'conditionType' => 'eq',
                        ]
                    ]
                ],
                ['text_attr' => 'something'],
                [
                    'setConditionType' => 'eq',
                    'setValue' => 'something',
                ],
            ],
            [
                [
                    'name' => 'text_attr',
                    'config' => [
                        'filter' => [
                            'filterType' => 'text',
                            'conditionType' => 'like',
                        ]
                    ]
                ],
                ['text_attr' => 'something'],
                [
                    'setConditionType' => 'like',
                    'setValue' => '%something%',
                ],
            ],
            [
                [
                    'name' => 'text_attr',
                    'config' => [
                        'filter' => [
                            'filterType' => 'text',
                            'conditionType' => 'like',
                            'valueExpression' => '%s%%'
                        ]
                    ]
                ],
                ['text_attr' => 'something'],
                [
                    'setConditionType' => 'like',
                    'setValue' => 'something%',
                ],
            ],
        ];
    }
}
