<?php

namespace Tests\Unit\CampaignPerformance;

use App\Services\CampaignPerformance\CampaignPerformanceFilters;
use App\Services\Filters\StickyDateRange;
use PHPUnit\Framework\TestCase;

/**
 * Campaign Performance after the date range moved out to StickyDateRange.
 *
 * AD-2613 rows #7/#11 required the range to reset on every fresh GET while the campaign and
 * creative selections survived it, implemented with a one-request DATES_APPLIED_FLASH
 * marker. The date half is now reversed to match production's legacy adc_from/adc_to
 * cookies; the campaign/creative half must be untouched, which is what this file pins.
 */
final class CampaignPerformanceStickyDatesTest extends TestCase
{
    public function testTheDateResetMarkerIsGone(): void
    {
        $this->assertFalse(
            defined(CampaignPerformanceFilters::class . '::DATES_APPLIED_FLASH'),
            'the flash marker existed only to reset the range on a fresh GET'
        );
    }

    /** The half of AD-2613 that still stands. */
    public function testNonDateSelectionsStillSurviveNormalize(): void
    {
        $out = CampaignPerformanceFilters::normalize([
            'date_start'       => '2026-04-01',
            'date_end'         => '2026-04-07',
            'campaign_ids'     => ['11', '22'],
            'creative_ids'     => ['33'],
            'conversion_types' => ['purchase'],
            'status'           => 'capped',
        ]);

        $this->assertSame(['11', '22'], $out['campaign_ids']);
        $this->assertSame(['33'], $out['creative_ids']);
        $this->assertSame(['purchase'], $out['conversion_types']);
        $this->assertSame('capped', $out['status']);
    }

    /**
     * The controller overlays the store's dates onto the session filters and re-normalizes.
     * That must move the dates without disturbing anything else.
     */
    public function testOverlayingTheStoresDatesLeavesTheRestIntact(): void
    {
        $session = CampaignPerformanceFilters::normalize([
            'date_start'   => '2020-01-01',
            'date_end'     => '2020-01-07',
            'campaign_ids' => ['11'],
            'status'       => 'capped',
        ]);

        $sticky = StickyDateRange::normalize(
            ['date_start' => '2026-04-01', 'date_end' => '2026-04-07', 'preset' => 'custom'],
            CampaignPerformanceFilters::defaults()
        );

        $session['date_start'] = $sticky['date_start'];
        $session['date_end']   = $sticky['date_end'];
        $out                   = CampaignPerformanceFilters::normalize($session);

        $this->assertSame('2026-04-01', $out['date_start']);
        $this->assertSame('2026-04-07', $out['date_end']);
        $this->assertSame(['11'], $out['campaign_ids']);
        $this->assertSame('capped', $out['status']);
        $this->assertArrayNotHasKey('preset', $out);
    }
}
