<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use App\Http\Resources\VehicleAction\FinanceFactory;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\FuelType;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;
use Tests\Data\FinanceSettingSeeder;
use Tests\TenantTestCase;

class FinanceFactorySantanderWidgetTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->seed(FinanceSettingSeeder::class);

        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Finance',
            'group' => 'Santander',
            'name' => 'Santander Finance Enabled',
            'config_key' => 'finance-santander-enabled',
            'type' => 'boolean',
            'value' => true,
        ]);
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Finance',
            'group' => 'Santander',
            'name' => 'Santander Widget Enabled',
            'config_key' => 'finance-santander-widget-enabled',
            'type' => 'boolean',
            'value' => false,
        ]);
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Finance',
            'group' => 'Santander',
            'name' => 'Santander Widget API Key',
            'config_key' => 'finance-santander-widget-key',
            'type' => 'secret',
            'value' => '',
        ]);
    }

    public function testFallsBackToDefaultFinanceCalculatorWhenWidgetDisabled()
    {
        Settings::update('finance-santander-widget-enabled', false);

        $vehicle = $this->createVehicle(['cap_code' => 'CAP123']);

        $result = new FinanceFactory()->widget($vehicle);

        $this->assertEquals('FinanceCalculator', $result['value']);
    }

    public function testWidgetPayloadForUsedVehicleUsesRegistrationNumber()
    {
        $this->enableWidget();
        $dealership = Dealership::factory()->create(['name' => 'Test Motors']);

        $vehicle = $this->createVehicle([
            'is_new' => false,
            'registration_number' => 'AB12CDE',
            'cap_code' => null,
            'price' => 12000,
            'odometer_mi' => 30000,
            'dealership_id' => $dealership->id,
        ]);

        $result = new FinanceFactory()->widget($vehicle);

        $this->assertEquals('SantanderCalculator', $result['value']);
        $this->assertEquals('scuk-widget-key', $result['data']['widget_key']);
        $this->assertEquals('Test Motors', $result['data']['dealer']);
        $this->assertEquals('USED', $result['data']['vehicle']['category']);
        $this->assertEquals('AB12CDE', $result['data']['vehicle']['registration']);
        $this->assertNull($result['data']['vehicle']['capCode']);
    }

    public function testWidgetPayloadForNewCarUsesCapCodeAndOmitsRegistration()
    {
        $this->enableWidget();

        $vehicle = $this->createVehicle([
            'is_new' => true,
            'registration_number' => null,
            'cap_code' => 'NEWCAP001',
            'price' => 30000,
        ]);

        $result = new FinanceFactory()->widget($vehicle);

        $this->assertEquals('SantanderCalculator', $result['value']);
        $this->assertEquals('NEW-CAR', $result['data']['vehicle']['category']);
        $this->assertEquals('NEWCAP001', $result['data']['vehicle']['capCode']);
        $this->assertNull($result['data']['vehicle']['registration']);
    }

    public function testWidgetPayloadForNewElectricVehicleUsesNewElectricCategory()
    {
        $this->enableWidget();

        $electric = FuelType::query()->firstOrCreate(['name' => 'Electric']);
        $vehicle = $this->createVehicle([
            'is_new' => true,
            'cap_code' => 'EV001',
            'fuel_type_id' => $electric->id,
        ]);

        $result = new FinanceFactory()->widget($vehicle);

        $this->assertEquals('NEW-ELECTRIC', $result['data']['vehicle']['category']);
    }

    public function testReturnsNullWhenWidgetEnabledButKeyIsEmpty()
    {
        Settings::update('finance-santander-widget-enabled', true);
        Settings::update('finance-santander-widget-key', '');

        $vehicle = $this->createVehicle(['cap_code' => 'CAP123']);

        $this->assertNull(new FinanceFactory()->widget($vehicle));
    }

    public function testReturnsNullForUsedVehicleWithoutRegistrationNumber()
    {
        $this->enableWidget();

        $vehicle = $this->createVehicle([
            'is_new' => false,
            'registration_number' => null,
            'cap_code' => null,
        ]);

        $this->assertNull(new FinanceFactory()->widget($vehicle));
    }

    private function enableWidget(): void
    {
        Settings::update('finance-santander-widget-enabled', true);
        Settings::update('finance-santander-widget-key', 'scuk-widget-key');
    }

    private function createVehicle(array $attributes = []): Vehicle
    {
        $make = VehicleMake::factory()->create();
        $model = VehicleModel::factory()->create();

        return Vehicle::factory()->create(array_merge([
            'make_id' => $make->id,
            'model_id' => $model->id,
            'price' => 20000,
            'odometer_mi' => 10000,
        ], $attributes));
    }
}
