<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use Illuminate\Support\Facades\Queue;
use Mtc\MercuryDataModels\Vehicle;
use Tests\SortingSettingSeed;
use Tests\TenantTestCase;

class SoldVehicleSortingTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        Queue::fake();
    }

    public function testSoldVehiclesSortedToEndWhenEnabled()
    {
        $this->seed(SortingSettingSeed::class);
        $this->enableSoldVehicleSorting();

        $soldCheap = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => true,
            'price' => 5000,
        ]);
        $availableExpensive = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => false,
            'price' => 30000,
        ]);
        $availableCheap = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => false,
            'price' => 10000,
        ]);
        $soldExpensive = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => true,
            'price' => 50000,
        ]);

        $response = $this->postJson(route('vehicles.index'), ['sort_by' => 'price-asc']);

        $response->assertStatus(200);
        $this->assertEquals($availableCheap->id, $response->json('results.data.0.id'));
        $this->assertEquals($availableExpensive->id, $response->json('results.data.1.id'));
        $this->assertEquals($soldCheap->id, $response->json('results.data.2.id'));
        $this->assertEquals($soldExpensive->id, $response->json('results.data.3.id'));
    }

    public function testSoldVehiclesNotSortedToEndWhenDisabled()
    {
        $this->seed(SortingSettingSeed::class);

        $soldCheap = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => true,
            'price' => 5000,
        ]);
        $availableExpensive = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => false,
            'price' => 30000,
        ]);

        $response = $this->postJson(route('vehicles.index'), ['sort_by' => 'price-asc']);

        $response->assertStatus(200);
        $this->assertEquals($soldCheap->id, $response->json('results.data.0.id'));
        $this->assertEquals($availableExpensive->id, $response->json('results.data.1.id'));
    }

    public function testSoldVehicleSortingWorksWithPriceDescending()
    {
        $this->seed(SortingSettingSeed::class);
        $this->enableSoldVehicleSorting();

        $soldExpensive = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => true,
            'price' => 50000,
        ]);
        $availableCheap = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => false,
            'price' => 10000,
        ]);
        $availableExpensive = Vehicle::factory()->create([
            'is_published' => true,
            'is_sold' => false,
            'price' => 30000,
        ]);

        $response = $this->postJson(route('vehicles.index'), ['sort_by' => 'price-desc']);

        $response->assertStatus(200);
        $this->assertEquals($availableExpensive->id, $response->json('results.data.0.id'));
        $this->assertEquals($availableCheap->id, $response->json('results.data.1.id'));
        $this->assertEquals($soldExpensive->id, $response->json('results.data.2.id'));
    }

    private function enableSoldVehicleSorting(): void
    {
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Filter',
            'group' => 'Sorting',
            'name' => 'Sort sold vehicles to end of results',
            'config_key' => 'filter-results-sort-sold-vehicles-last',
            'type' => 'boolean',
            'value' => true,
        ]);
    }
}
