<?php

namespace Tests\Tenant;

use App\ImportMaps\VehicleImportMap;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class VehicleImportMapTest extends TenantTestCase
{
    public function testName()
    {
        $this->assertEquals('Vehicles', (new VehicleImportMap())->name());
    }

    public function testColumnsReturnsArray()
    {
        $columns = (new VehicleImportMap())->columns();
        $this->assertIsArray($columns);
        $this->assertNotEmpty($columns);
    }

    public function testColumnsContainsCoreFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $expectedKeys = [
            'uuid',
            'registration_number',
            'vin',
            'is_published',
            'is_new',
            'title',
            'derivative',
            'make_id',
            'model_id',
            'price',
            'colour',
            'odometer_mi',
            'odometer_km',
            'images',
            'features',
        ];

        foreach ($expectedKeys as $key) {
            $this->assertArrayHasKey($key, $columns, "Missing column: $key");
        }
    }

    public function testColumnsContainsEvFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $evKeys = [
            'battery_range',
            'battery_capacity_kwh',
            'battery_usable_capacity_kwh',
            'battery_charge_time',
            'battery_quick_charge_time',
        ];

        foreach ($evKeys as $key) {
            $this->assertArrayHasKey($key, $columns, "Missing EV column: $key");
        }
    }

    public function testColumnsContainsTaxonomyFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $taxonomyKeys = [
            'fuel_type_id',
            'drivetrain_id',
            'transmission_id',
            'body_style_id',
            'dealership_id',
        ];

        foreach ($taxonomyKeys as $key) {
            $this->assertArrayHasKey($key, $columns, "Missing taxonomy column: $key");
        }
    }

    public function testColumnsContainsBooleanFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $booleanKeys = [
            'is_published',
            'is_sold',
            'is_new',
            'is_demo',
            'is_reserved',
            'is_vat_applicable',
        ];

        foreach ($booleanKeys as $key) {
            $this->assertArrayHasKey($key, $columns, "Missing boolean column: $key");
        }
    }

    public function testColumnsContainsVideoFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $this->assertArrayHasKey('main_video_url', $columns);
        $this->assertArrayHasKey('exterior_video_url', $columns);
        $this->assertArrayHasKey('interior_video_url', $columns);
    }

    public function testColumnsContainsAutoTraderPublishFields()
    {
        $columns = (new VehicleImportMap())->columns();

        $this->assertArrayHasKey('autotrader_enabled', $columns);
        $this->assertArrayHasKey('publish_advert', $columns);
        $this->assertArrayHasKey('publish_profile', $columns);
    }

    public function testCastRestrictionsReturnsBoolForBoolFields()
    {
        $map = new VehicleImportMap();

        $this->assertEquals('bool', $map->castRestrictions('is_published'));
        $this->assertEquals('bool', $map->castRestrictions('is_sold'));
        $this->assertEquals('bool', $map->castRestrictions('is_new'));
        $this->assertEquals('bool', $map->castRestrictions('is_demo'));
        $this->assertEquals('bool', $map->castRestrictions('is_reserved'));
        $this->assertEquals('bool', $map->castRestrictions('personalized_number_plate'));
    }

    public function testCastRestrictionsReturnsStringForUuid()
    {
        $map = new VehicleImportMap();
        $this->assertEquals('string', $map->castRestrictions('uuid'));
    }

    public function testCastRestrictionsReturnsDatetimeForDateFields()
    {
        $map = new VehicleImportMap();
        $this->assertEquals('datetime', $map->castRestrictions('available_date'));
        $this->assertEquals('datetime', $map->castRestrictions('stock_arrival_date'));
    }

    public function testCastRestrictionsReturnsNullForUnknownField()
    {
        $map = new VehicleImportMap();
        $this->assertNull($map->castRestrictions('price'));
        $this->assertNull($map->castRestrictions('colour'));
        $this->assertNull($map->castRestrictions('nonexistent_field'));
    }

    public function testQueryReturnsVehicleBuilder()
    {
        $map = new VehicleImportMap();
        $query = $map->query();

        $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $query);
        $this->assertEquals(Vehicle::class, get_class($query->getModel()));
    }
}
