<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use App\Services\VehicleBulkUpdateService;
use Database\Seeders\Tenant\FinanceProductRestrictionSetting;
use Database\Seeders\Tenant\FinanceSettingSeeder;
use Illuminate\Support\Facades\Bus;
use Mtc\MercuryDataModels\Finance\Jobs\FetchVehicleFinanceData;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class VehicleBulkUpdateServiceTest extends TenantTestCase
{
    public function testRequestFinanceQuotesSkipsVehiclesWithStopFinanceSync(): void
    {
        Bus::fake();

        $this->seed(FinanceSettingSeeder::class);
        $this->seed(FinanceProductRestrictionSetting::class);
        Settings::update('finance-codeweavers-enabled', true);
        Settings::update('finance-codeweavers-api_key', 'test-key');
        Settings::update('finance-codeweavers-username', 'test-user');
        Settings::update('finance-codeweavers-password', 'test-pass');

        $optedIn = Vehicle::factory()->create([
            'price' => 10000,
            'cap_id' => '11111',
            'data' => [],
        ]);

        $optedOut = Vehicle::factory()->create([
            'price' => 10000,
            'cap_id' => '22222',
            'data' => ['stop_finance_sync' => true],
        ]);

        $result = new VehicleBulkUpdateService()
            ->requestFinanceQuotes([$optedIn->id, $optedOut->id]);

        $this->assertEquals(1, $result['requested']);
        $this->assertEquals(1, $result['failed']);

        Bus::assertDispatchedTimes(FetchVehicleFinanceData::class, 1);
    }
}
