<?php

namespace Tests\Tenant;

use App\Services\MeilisearchService;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleFeature;
use Tests\TenantTestCase;

class MeilisearchTransformVehicleTest extends TenantTestCase
{
    public function test_transform_vehicle_indexes_feature_names_as_searchable_string(): void
    {
        $vehicle = Vehicle::factory()->create();
        VehicleFeature::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'name' => 'Heated Seats',
        ]);
        VehicleFeature::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'name' => 'Panoramic Sunroof',
        ]);

        $document = app(MeilisearchService::class)->transformVehicle($vehicle->fresh());

        $this->assertArrayHasKey('features', $document);
        $this->assertStringContainsString('Heated Seats', $document['features']);
        $this->assertStringContainsString('Panoramic Sunroof', $document['features']);
        $this->assertCount(2, $document['feature_ids']);
    }

    public function test_features_is_registered_as_searchable_attribute(): void
    {
        $service = app(MeilisearchService::class);
        $reflection = new \ReflectionMethod($service, 'getSearchableAttributes');
        $reflection->setAccessible(true);

        $this->assertContains('features', $reflection->invoke($service));
    }

    public function test_published_at_is_registered_as_sortable_attribute(): void
    {
        $service = app(MeilisearchService::class);
        $reflection = new \ReflectionMethod($service, 'getSortableAttributes');
        $reflection->setAccessible(true);

        $this->assertContains('published_at', $reflection->invoke($service));
    }

    public function test_transform_vehicle_includes_published_at(): void
    {
        $vehicle = Vehicle::factory()->create();

        $document = app(MeilisearchService::class)->transformVehicle($vehicle->fresh());

        $this->assertArrayHasKey('published_at', $document);
    }
}
