<?php

namespace Tests\Feature;

use App\MeilisearchFilter;
use Tests\TenantTestCase;

class MeilisearchSortTest extends TenantTestCase
{
    /**
     * New arrivals ("latest") must order by published_at so the SRP reflects when a
     * vehicle went live, matching the database NewArrivals sort - not created_at.
     */
    public function testNewArrivalsSortUsesPublishedAt(): void
    {
        $this->assertMeilisearchSort('latest', ['published_at:desc']);
    }

    public function testOldestSortStillUsesCreatedAt(): void
    {
        $this->assertMeilisearchSort('oldest', ['created_at:asc']);
    }

    /**
     * @param array<int, string> $expected
     */
    private function assertMeilisearchSort(string $sortOption, array $expected): void
    {
        $filter = $this->app->make(MeilisearchFilter::class);

        $reflection = new \ReflectionClass($filter);

        $property = $reflection->getProperty('active_sort_option_name');
        $property->setAccessible(true);
        $property->setValue($filter, $sortOption);

        $method = $reflection->getMethod('getMeilisearchSort');
        $method->setAccessible(true);

        $this->assertSame($expected, $method->invoke($filter));
    }
}
