<?php

namespace Tests\Tenant\Reporting;

use App\Facades\Settings;
use App\Reporting\StatsModels\VehicleCount;
use App\Reporting\VehiclesInStockKPI;
use Carbon\Carbon;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class ExcludeSoldVehiclesReportingTest extends TenantTestCase
{
    public function testVehicleCountStatIncludesSoldByDefault(): void
    {
        Vehicle::factory(2)->create(['is_published' => 1, 'is_sold' => false]);
        Vehicle::factory(3)->create(['is_published' => 1, 'is_sold' => true]);

        $this->assertSame(5, (new VehicleCount())->getValue(Carbon::now()));
    }

    public function testVehicleCountStatExcludesSoldWhenSettingEnabled(): void
    {
        $this->enableExcludeSoldVehicles();

        Vehicle::factory(2)->create(['is_published' => 1, 'is_sold' => false]);
        Vehicle::factory(3)->create(['is_published' => 1, 'is_sold' => true]);

        $this->assertSame(2, (new VehicleCount())->getValue(Carbon::now()));
    }

    public function testVehiclesPublishedIncludesSoldByDefault(): void
    {
        Vehicle::factory(2)->create(['is_published' => 1, 'is_sold' => false]);
        Vehicle::factory(3)->create(['is_published' => 1, 'is_sold' => true]);

        $this->assertSame(5, (new VehiclesInStockKPI())->toArray()['value']);
    }

    public function testVehiclesPublishedExcludesSoldWhenSettingEnabled(): void
    {
        $this->enableExcludeSoldVehicles();

        Vehicle::factory(2)->create(['is_published' => 1, 'is_sold' => false]);
        Vehicle::factory(3)->create(['is_published' => 1, 'is_sold' => true]);

        $this->assertSame(2, (new VehiclesInStockKPI())->toArray()['value']);
    }

    private function enableExcludeSoldVehicles(): void
    {
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Filter',
            'group' => 'Sold Vehicles',
            'name' => 'Exclude sold vehicles from listings',
            'config_key' => 'filter-results-exclude-sold-vehicles',
            'type' => 'boolean',
            'value' => true,
        ]);
    }
}
