<?php

namespace Tests\Tenant;

use App\Traits\EnsuresVehicleAttribute;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleAttribute;
use Mtc\MercuryDataModels\VehicleAttributeValue;
use Tests\TenantTestCase;

class VehicleAttributeTraitTest extends TenantTestCase
{
    use EnsuresVehicleAttribute;

    public function testEmptyAttributes()
    {
        $this->assertEmpty(VehicleAttribute::all());
    }

    public function testAttribute()
    {
        $this->getVehicleAttribute('example_attribute', 'boolean');

        $this->assertCount(1, VehicleAttribute::all());
        $this->assertEquals('example_attribute', VehicleAttribute::first()->name);
    }

    public function testSaveAttributeValueUsesZeroBasedIndex()
    {
        $vehicle = Vehicle::factory()->create();
        $attribute = VehicleAttribute::factory()->create([
            'name' => 'test_attribute',
            'slug' => 'test_attribute',
            'type' => 'text',
            'model' => 'vehicle',
            'count' => 1,
        ]);

        // Simulate data coming in with a non-zero array key (like from allocateAttributeValues)
        $attributeData = [
            'id' => $attribute->id,
            'slug' => $attribute->slug,
            'type' => $attribute->type,
            'value' => [
                5 => ['id' => null, 'value' => 'test value'], // key is 5, not 0
            ],
        ];

        $this->saveAttributeValue($vehicle, $attributeData);

        $savedValue = VehicleAttributeValue::where('owner_id', $vehicle->id)
            ->where('attribute_id', $attribute->id)
            ->first();

        $this->assertNotNull($savedValue);
        $this->assertEquals(0, $savedValue->index, 'Index should be 0, not the original array key');
        $this->assertEquals('test value', $savedValue->value_text);
    }

    public function testSaveAttributeValuePreservesSequentialIndicesForMultiValue()
    {
        $vehicle = Vehicle::factory()->create();
        $attribute = VehicleAttribute::factory()->create([
            'name' => 'multi_value_attribute',
            'slug' => 'multi_value_attribute',
            'type' => 'text',
            'model' => 'vehicle',
            'count' => 3,
        ]);

        // Simulate data with non-sequential keys
        $attributeData = [
            'id' => $attribute->id,
            'slug' => $attribute->slug,
            'type' => $attribute->type,
            'value' => [
                3 => ['id' => null, 'value' => 'first'],
                7 => ['id' => null, 'value' => 'second'],
                9 => ['id' => null, 'value' => 'third'],
            ],
        ];

        $this->saveAttributeValue($vehicle, $attributeData);

        $savedValues = VehicleAttributeValue::where('owner_id', $vehicle->id)
            ->where('attribute_id', $attribute->id)
            ->orderBy('index')
            ->get();

        $this->assertCount(3, $savedValues);
        $this->assertEquals(0, $savedValues[0]->index);
        $this->assertEquals(1, $savedValues[1]->index);
        $this->assertEquals(2, $savedValues[2]->index);
        $this->assertEquals('first', $savedValues[0]->value_text);
        $this->assertEquals('second', $savedValues[1]->value_text);
        $this->assertEquals('third', $savedValues[2]->value_text);
    }
}
