<?php

namespace Tests\Tenant;

use App\Imports\AutoTraderApiToVehicleImport;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Tests\TenantTestCase;

class AutoTraderApiToVehicleImportTest extends TenantTestCase
{
    protected AutoTraderApiToVehicleImport $importer;

    #[\Override]
    protected function setUp(): void
    {
        parent::setUp();
        $this->importer = new AutoTraderApiToVehicleImport();
        $this->importer->setDealershipId(1); // example dealership
    }

    public function testExistsReturnsTrueWhenVehicleWithUuidExists()
    {
        Vehicle::factory()->create([
            'stock_provider' => 'auto-trader',
            'uuid' => 'autotrader-uuid-123',
        ]);

        $vehicleData = [
            'metadata' => ['stockId' => 'autotrader-uuid-123'],
            'vehicle' => ['registration' => 'AB12CDE', 'vin' => 'VIN123']
        ];

        $this->assertTrue($this->importer->exists($vehicleData));
    }

    public function testExistsReturnsTrueWhenVehicleWithVinExists()
    {
        Vehicle::factory()->create([
            'stock_provider' => 'auto-trader',
            'vin' => 'VIN123'
        ]);

        $vehicleData = [
            'metadata' => ['stockId' => 'non-matching-id'],
            'vehicle' => ['registration' => 'AB12CDE', 'vin' => 'VIN123']
        ];

        $this->assertTrue($this->importer->exists($vehicleData));
    }

    public function testMapChargeTimesExtractsSlowAndQuickChargeData(): void
    {
        $chargeTimes = [
            [
                'chargerType' => 'Fast Charge',
                'chargerDescription' => '22 kW',
                'chargerSupplyType' => 'AC Three Phase',
                'connectorType' => 'Type 2',
                'startBatteryPercentage' => 0,
                'endBatteryPercentage' => 100,
                'durationMinutes' => 305,
            ],
            [
                'chargerType' => 'Fast Charge',
                'chargerDescription' => '22 kW',
                'chargerSupplyType' => 'AC Three Phase',
                'connectorType' => 'Type 2',
                'startBatteryPercentage' => 10,
                'endBatteryPercentage' => 100,
                'durationMinutes' => 275,
            ],
            [
                'chargerType' => 'Rapid Charge',
                'chargerDescription' => '50 kW',
                'chargerSupplyType' => 'DC',
                'connectorType' => 'CCS',
                'startBatteryPercentage' => 0,
                'endBatteryPercentage' => 80,
                'durationMinutes' => 66,
            ],
            [
                'chargerType' => 'Rapid Charge',
                'chargerDescription' => '50 kW',
                'chargerSupplyType' => 'DC',
                'connectorType' => 'CCS',
                'startBatteryPercentage' => 10,
                'endBatteryPercentage' => 80,
                'durationMinutes' => 58,
            ],
        ];

        $reflection = new \ReflectionMethod($this->importer, 'mapChargeTimes');
        $reflection->setAccessible(true);
        $result = $reflection->invoke($this->importer, $chargeTimes);

        // Slow charge (AC) - picks lowest duration (275 over 305)
        $this->assertEquals(275, $result['battery_charge_time']);
        $this->assertEquals('Type 2', $result['plug_type']);
        $this->assertEquals('22 kW AC Three Phase', $result['battery_slow_charge_description']);

        // Quick charge (DC) - picks lowest duration (58 over 66)
        $this->assertEquals(58, $result['battery_quick_charge_time']);
        $this->assertEquals('CCS', $result['rapid_charge_plug_type']);
        $this->assertEquals(80, $result['battery_quick_charge_level']);
        $this->assertEquals(10, $result['battery_quick_charge_start_level']);
        $this->assertEquals('50 kW DC', $result['battery_quick_charge_description']);
    }

    public function testMapChargeTimesReturnsEmptyArrayWhenNoData(): void
    {
        $reflection = new \ReflectionMethod($this->importer, 'mapChargeTimes');
        $reflection->setAccessible(true);

        $this->assertEmpty($reflection->invoke($this->importer, []));
    }
}
