<?php

namespace Tests\Unit\Imports;

use App\Imports\AutoTraderApiToVehicleImport;
use Tests\TestCase;

class AutoTraderMergeFittedFeaturesTest extends TestCase
{
    public function testStandardFeaturesFromTaxonomyPassThroughUnchanged(): void
    {
        $taxonomy = [
            ['type' => 'Standard', 'name' => 'ABS', 'category' => 'Safety', 'basicPrice' => 0, 'vatPrice' => 0],
            ['type' => 'Standard', 'name' => 'Air Con', 'category' => 'Comfort', 'basicPrice' => 0, 'vatPrice' => 0],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices([], $taxonomy);

        $this->assertCount(2, $result);
        $this->assertSame('ABS', $result[0]['name']);
        $this->assertSame('Air Con', $result[1]['name']);
    }

    public function testFittedOptionalFeaturesReceivePricesFromTaxonomy(): void
    {
        $fitted = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior'],
        ];
        $taxonomy = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior', 'basicPrice' => 1500, 'vatPrice' => 300],
            ['type' => 'Optional', 'name' => 'Heated Steering Wheel', 'category' => 'Comfort', 'basicPrice' => 200, 'vatPrice' => 40],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(1, $result);
        $this->assertSame('Panoramic Roof', $result[0]['name']);
        $this->assertSame(1500, $result[0]['basicPrice']);
        $this->assertSame(300, $result[0]['vatPrice']);
        $this->assertSame('Exterior', $result[0]['category']);
    }

    public function testOptionalFeaturesInTaxonomyButNotFittedAreDropped(): void
    {
        $fitted = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior'],
        ];
        $taxonomy = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior', 'basicPrice' => 1500, 'vatPrice' => 300],
            ['type' => 'Optional', 'name' => 'Heated Steering Wheel', 'category' => 'Comfort', 'basicPrice' => 200, 'vatPrice' => 40],
            ['type' => 'Optional', 'name' => 'Sport Pack', 'category' => 'Performance', 'basicPrice' => 2500, 'vatPrice' => 500],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(1, $result);
        $this->assertSame('Panoramic Roof', $result[0]['name']);
    }

    public function testFittedOptionalWithoutTaxonomyMatchIsIncludedWithNullPrices(): void
    {
        $fitted = [
            ['type' => 'Optional', 'name' => 'Dealer Fitted Roof Bars', 'category' => 'Accessories'],
        ];
        $taxonomy = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior', 'basicPrice' => 1500, 'vatPrice' => 300],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(1, $result);
        $this->assertSame('Dealer Fitted Roof Bars', $result[0]['name']);
        $this->assertNull($result[0]['basicPrice']);
        $this->assertNull($result[0]['vatPrice']);
        $this->assertSame('Accessories', $result[0]['category']);
    }

    public function testMatchingIsCaseInsensitiveAndIgnoresWhitespace(): void
    {
        $fitted = [
            ['type' => 'Optional', 'name' => '  panoramic roof  ', 'category' => 'Exterior'],
        ];
        $taxonomy = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior', 'basicPrice' => 1500, 'vatPrice' => 300],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(1, $result);
        $this->assertSame(1500, $result[0]['basicPrice']);
    }

    public function testEmptyInputsReturnEmptyResult(): void
    {
        $this->assertSame([], AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices([], []));
    }

    public function testStandardAndOptionalAreCombinedInOutput(): void
    {
        $fitted = [
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior'],
            ['type' => 'Standard', 'name' => 'Ignored Standard', 'category' => 'Safety'],
        ];
        $taxonomy = [
            ['type' => 'Standard', 'name' => 'ABS', 'category' => 'Safety', 'basicPrice' => 0, 'vatPrice' => 0],
            ['type' => 'Optional', 'name' => 'Panoramic Roof', 'category' => 'Exterior', 'basicPrice' => 1500, 'vatPrice' => 300],
            ['type' => 'Optional', 'name' => 'Sport Pack', 'category' => 'Performance', 'basicPrice' => 2500, 'vatPrice' => 500],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(2, $result);
        $this->assertSame('ABS', $result[0]['name']);
        $this->assertSame('Panoramic Roof', $result[1]['name']);
        $this->assertSame(1500, $result[1]['basicPrice']);
    }

    public function testFittedStandardEntriesAreIgnoredSinceStandardComesFromTaxonomy(): void
    {
        $fitted = [
            ['type' => 'Standard', 'name' => 'Only In Fitted', 'category' => 'Safety'],
        ];
        $taxonomy = [
            ['type' => 'Standard', 'name' => 'ABS', 'category' => 'Safety', 'basicPrice' => 0, 'vatPrice' => 0],
        ];

        $result = AutoTraderApiToVehicleImport::mergeFittedFeaturesWithTaxonomyPrices($fitted, $taxonomy);

        $this->assertCount(1, $result);
        $this->assertSame('ABS', $result[0]['name']);
    }
}
