<?php

/*
 * This file is part of the Predis package.
 *
 * (c) 2009-2020 Daniele Alessandri
 * (c) 2021-2026 Till Krüss
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Predis\Command\Redis\TimeSeries;

use Predis\Command\Argument\TimeSeries\CommonArguments;
use Predis\Command\Argument\TimeSeries\CreateArguments;
use Predis\Command\Argument\TimeSeries\MRangeArguments;
use Predis\Command\Argument\TimeSeries\RangeArguments;
use Predis\Command\Redis\PredisCommandTestCase;
use UnexpectedValueException;

/**
 * @group commands
 * @group realm-stack
 */
class TSMREVRANGE_Test extends PredisCommandTestCase
{
    /**
     * {@inheritDoc}
     */
    protected function getExpectedCommand(): string
    {
        return TSMREVRANGE::class;
    }

    /**
     * {@inheritDoc}
     */
    protected function getExpectedId(): string
    {
        return 'TSMREVRANGE';
    }

    /**
     * @group disconnected
     * @dataProvider argumentsProvider
     */
    public function testFilterArguments(array $actualArguments, array $expectedArguments): void
    {
        $command = $this->getCommand();
        $command->setArguments($actualArguments);

        $this->assertSameValues($expectedArguments, $command->getArguments());
    }

    /**
     * @group disconnected
     */
    public function testParseResponse(): void
    {
        $this->assertSame(1, $this->getCommand()->parseResponse(1));
    }

    /**
     * @group disconnected
     * @dataProvider parseResponseProvider
     */
    public function testParseResponsePassesThroughSingleAndMultipleAggregatorResults(array $response): void
    {
        $this->assertSame($response, $this->getCommand()->parseResponse($response));
    }

    public function parseResponseProvider(): array
    {
        return [
            'single aggregator' => [
                [
                    ['stock:A', [['type', 'stock'], ['name', 'A']], [[2000, '210'], [1000, '110']]],
                ],
            ],
            'multiple aggregators' => [
                [
                    ['stock:A', [['type', 'stock'], ['name', 'A']], [[2000, '210', '3'], [1000, '110', '2']]],
                ],
            ],
        ];
    }

    /**
     * @group disconnected
     */
    public function testThrowsOnGroupByCombinedWithMultipleAggregators(): void
    {
        $mrangeArguments = (new MRangeArguments())
            ->aggregation([RangeArguments::AGG_MIN, RangeArguments::AGG_MAX], 1000)
            ->filter('type=stock');

        $this->expectException(UnexpectedValueException::class);
        $this->expectExceptionMessage('GROUPBY cannot be combined with multiple aggregators.');

        $mrangeArguments->groupBy('type', 'max');
    }

    /**
     * @group connected
     * @group relay-resp3
     * @return void
     * @requiresRedisTimeSeriesVersion >= 1.4.0
     */
    public function testQueryRangeAcrossMultipleTimeSeriesInReverseDirection(): void
    {
        $redis = $this->getClient();
        $expectedResponse = [
            [
                'type=stock',
                [
                    ['type', 'stock'],
                    ['__reducer__', 'max'],
                    ['__source__', 'stock:A,stock:B'],
                ],
                [
                    [1020, '120'],
                    [1010, '110'],
                    [1000, '120'],
                ],
            ],
        ];

        $this->assertEquals(
            'OK',
            $redis->tscreate('stock:A', (new CreateArguments())->labels('type', 'stock', 'name', 'A'))
        );
        $this->assertEquals(
            'OK',
            $redis->tscreate('stock:B', (new CreateArguments())->labels('type', 'stock', 'name', 'B'))
        );
        $this->assertSame(
            [1000, 1010, 1020],
            $redis->tsmadd('stock:A', 1000, 100, 'stock:A', 1010, 110, 'stock:A', 1020, 120)
        );
        $this->assertSame(
            [1000, 1010, 1020],
            $redis->tsmadd('stock:B', 1000, 120, 'stock:B', 1010, 110, 'stock:B', 1020, 100)
        );

        $mrangeArguments = (new MRangeArguments())
            ->withLabels()
            ->filter('type=stock')
            ->groupBy('type', 'max');

        $this->assertEquals($expectedResponse, $redis->tsmrevrange('-', '+', $mrangeArguments));
    }

    /**
     * @group connected
     * @return void
     * @requiresRedisTimeSeriesVersion >= 1.10.0
     */
    public function testQueryRangeAcrossMultipleTimeSeriesInReverseDirectionResp3(): void
    {
        $redis = $this->getResp3Client();
        $expectedResponse = [
            'type=stock' => [
                ['type' => 'stock'],
                ['reducers' => ['max']],
                ['sources' => ['stock:A', 'stock:B']],
                [
                    [1020, 120],
                    [1010, 110],
                    [1000, 120],
                ],
            ],
        ];

        $this->assertEquals(
            'OK',
            $redis->tscreate('stock:A', (new CreateArguments())->labels('type', 'stock', 'name', 'A'))
        );
        $this->assertEquals(
            'OK',
            $redis->tscreate('stock:B', (new CreateArguments())->labels('type', 'stock', 'name', 'B'))
        );
        $this->assertSame(
            [1000, 1010, 1020],
            $redis->tsmadd('stock:A', 1000, 100, 'stock:A', 1010, 110, 'stock:A', 1020, 120)
        );
        $this->assertSame(
            [1000, 1010, 1020],
            $redis->tsmadd('stock:B', 1000, 120, 'stock:B', 1010, 110, 'stock:B', 1020, 100)
        );

        $mrangeArguments = (new MRangeArguments())
            ->withLabels()
            ->filter('type=stock')
            ->groupBy('type', 'max');

        $this->assertEquals($expectedResponse, $redis->tsmrevrange('-', '+', $mrangeArguments));
    }

    /**
     * @group connected
     * @group relay-resp3
     * @return void
     * @requiresRedisVersion >= 8.5.0
     */
    public function testAddSamplesIntoFewTimeSeriesWithNaNValues(): void
    {
        $redis = $this->getClient();

        $createArguments = (new CreateArguments())
            ->retentionMsecs(60000)
            ->duplicatePolicy(CommonArguments::POLICY_MAX)
            ->labels('type', 'temperature', 'name', 'A');

        $this->assertEquals(
            'OK',
            $redis->tscreate('temperature:A', $createArguments)
        );

        $createArguments = (new CreateArguments())
            ->retentionMsecs(60000)
            ->duplicatePolicy(CommonArguments::POLICY_MAX)
            ->labels('type', 'temperature', 'name', 'B');

        $this->assertEquals(
            'OK',
            $redis->tscreate('temperature:B', $createArguments)
        );

        // Add NaN value samples
        $this->assertEquals(
            [1000, 1001],
            $redis->tsmadd(
                'temperature:A', 1000, 'NaN', 'temperature:A', 1001, 27
            )
        );

        $this->assertEquals(
            [1000, 1001],
            $redis->tsmadd(
                'temperature:B', 1000, 'NaN', 'temperature:B', 1001, 28
            )
        );

        // Ensure that we can count all values (included NaN)
        $mRangeArguments = (new MRangeArguments())
            ->aggregation(RangeArguments::AGG_COUNT_ALL, 1000)
            ->filter('type=temperature');

        $expectedResponse = [
            ['temperature:A', [], [[1000, 2]]],
            ['temperature:B', [], [[1000, 2]]],
        ];

        $this->assertEquals($expectedResponse, $redis->tsmrevrange(1000, 1001, $mRangeArguments));

        // Ensure that we can count NaN values only
        $mRangeArguments = (new MRangeArguments())
            ->aggregation(RangeArguments::AGG_COUNT_NAN, 1000)
            ->filter('type=temperature');

        $expectedResponse = [
            ['temperature:A', [], [[1000, 1]]],
            ['temperature:B', [], [[1000, 1]]],
        ];

        $this->assertEquals($expectedResponse, $redis->tsmrevrange(1000, 1001, $mRangeArguments));
    }

    /**
     * @group connected
     * @group relay-resp3
     * @return void
     * @requiresRedisVersion >= 8.7.2
     */
    public function testQueryMultipleTimeSeriesInReverseWithMultipleAggregators(): void
    {
        $redis = $this->getClient();

        $this->assertEquals(
            'OK',
            $redis->tscreate('stock:A', (new CreateArguments())->labels('type', 'stock', 'name', 'A'))
        );
        $this->assertSame(
            [1000, 1010, 1020],
            $redis->tsmadd('stock:A', 1000, 100, 'stock:A', 1010, 110, 'stock:A', 1020, 120)
        );

        $mrangeArguments = (new MRangeArguments())
            ->aggregation([RangeArguments::AGG_MIN, RangeArguments::AGG_MAX], 1000)
            ->filter('type=stock');

        $response = $redis->tsmrevrange('-', '+', $mrangeArguments);

        $this->assertCount(1, $response);
        $this->assertSame('stock:A', $response[0][0]);
        $samples = $response[0][2];
        $this->assertCount(1, $samples);
        $this->assertCount(3, $samples[0]);
        $this->assertSame(1000, $samples[0][0]);
    }

    public function argumentsProvider(): array
    {
        return [
            'with default arguments' => [
                [1000, 1001, (new MRangeArguments())->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with LATEST modifier' => [
                [1000, 1001, (new MRangeArguments())->latest()->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'LATEST', 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with FILTER_BY_TS modifier' => [
                [1000, 1001, (new MRangeArguments())->filterByTs(1000, 1001)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'FILTER_BY_TS', 1000, 1001, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with FILTER_BY_VALUE modifier' => [
                [1000, 1001, (new MRangeArguments())->filterByValue(1000, 1001)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'FILTER_BY_VALUE', 1000, 1001, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with WITHLABELS modifier' => [
                [1000, 1001, (new MRangeArguments())->withLabels()->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'WITHLABELS', 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with SELECTED_LABELS modifier' => [
                [1000, 1001, (new MRangeArguments())->selectedLabels('label1', 'label2')->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'SELECTED_LABELS', 'label1', 'label2', 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with COUNT modifier' => [
                [1000, 1001, (new MRangeArguments())->count(2)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'COUNT', 2, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - default arguments' => [
                [1000, 1001, (new MRangeArguments())->aggregation('sum', 2)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'AGGREGATION', 'sum', 2, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - with ALIGN' => [
                [1000, 1001, (new MRangeArguments())->aggregation('sum', 2, 2)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'ALIGN', 2, 'AGGREGATION', 'sum', 2, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - with BUCKETTIMESTAMP' => [
                [1000, 1001, (new MRangeArguments())->aggregation('sum', 2, 0, 10000)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'AGGREGATION', 'sum', 2, 'BUCKETTIMESTAMP', 10000, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - with EMPTY' => [
                [1000, 1001, (new MRangeArguments())->aggregation('sum', 2, 0, 0, true)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'AGGREGATION', 'sum', 2, 'EMPTY', 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - multiple aggregators as array' => [
                [1000, 1001, (new MRangeArguments())->aggregation(['min', 'max'], 2)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'AGGREGATION', 'min,max', 2, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - multiple aggregators as string' => [
                [1000, 1001, (new MRangeArguments())->aggregation('min,max', 2)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'AGGREGATION', 'min,max', 2, 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with AGGREGATION modifier - multiple aggregators with all options' => [
                [1000, 1001, (new MRangeArguments())->aggregation(['min', 'max', 'avg'], 2, 2, 10000, true)->filter('filterExpression1', 'filterExpression2')],
                [1000, 1001, 'ALIGN', 2, 'AGGREGATION', 'min,max,avg', 2, 'BUCKETTIMESTAMP', 10000, 'EMPTY', 'FILTER', 'filterExpression1', 'filterExpression2'],
            ],
            'with GROUPBY modifier' => [
                [1000, 1001, (new MRangeArguments())->filter('filterExpression1', 'filterExpression2')->groupBy('label', 'reducer')],
                [1000, 1001, 'FILTER', 'filterExpression1', 'filterExpression2', 'GROUPBY', 'label', 'REDUCE', 'reducer'],
            ],
            'with all modifiers' => [
                [1000, 1001, (new MRangeArguments())->latest()->filterByTs(1000, 1001)->filterByValue(1000, 1001)->withLabels()->count(2)->aggregation('sum', 2)->filter('filterExpression1', 'filterExpression2')->groupBy('label', 'reducer'), 'filterExpression1', 'filterExpression2'],
                [1000, 1001, 'LATEST', 'FILTER_BY_TS', 1000, 1001, 'FILTER_BY_VALUE', 1000, 1001, 'WITHLABELS', 'COUNT', 2, 'AGGREGATION', 'sum', 2, 'FILTER', 'filterExpression1', 'filterExpression2', 'GROUPBY', 'label', 'REDUCE', 'reducer'],
            ],
        ];
    }
}
