<?php

namespace Tests\Tenant;

use App\Imports\YammaYapEquipmentImport;
use Maatwebsite\Excel\Facades\Excel;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleSpecType;
use Mtc\MercuryDataModels\VehicleStandardEquipment;
use Tests\TenantTestCase;

class YammaYapEquipmentImportTest extends TenantTestCase
{
    private function createCsv(array $rows): string
    {
        $path = tempnam(sys_get_temp_dir(), 'yammayap') . '.csv';
        $handle = fopen($path, 'w');
        fputcsv($handle, ['cdk_number', 'interior_features', 'exterior_features']);
        foreach ($rows as $row) {
            fputcsv($handle, $row);
        }
        fclose($handle);

        return $path;
    }

    public function testImportsInteriorAndExteriorWithCorrectCategories()
    {
        $vehicle = Vehicle::factory()->create(['uuid' => 'TEST-001']);

        $path = $this->createCsv([
            ['TEST-001', 'Heated seats,Leather steering wheel', 'LED headlights,Tow bar'],
        ]);

        $import = new YammaYapEquipmentImport(',');
        Excel::import($import, $path);

        $this->assertEquals(4, $import->imported);
        $this->assertEquals(0, $import->skippedRows);

        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Heated seats',
            'category' => 'Interior',
            'type' => VehicleSpecType::STANDARD_EQUIPMENT->value,
        ]);
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Leather steering wheel',
            'category' => 'Interior',
        ]);
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'LED headlights',
            'category' => 'Exterior',
        ]);
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Tow bar',
            'category' => 'Exterior',
        ]);

        unlink($path);
    }

    public function testDeletesExistingNullInteriorAndExteriorCategories()
    {
        $vehicle = Vehicle::factory()->create(['uuid' => 'TEST-002']);

        // Pre-existing equipment that should be deleted
        VehicleStandardEquipment::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'description' => 'Old interior item',
            'category' => 'Interior',
        ]);
        VehicleStandardEquipment::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'description' => 'Old exterior item',
            'category' => 'Exterior',
        ]);
        VehicleStandardEquipment::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'description' => 'Uncategorised item',
            'category' => null,
        ]);

        // Pre-existing equipment that should be preserved
        VehicleStandardEquipment::factory()->create([
            'vehicle_id' => $vehicle->id,
            'vehicle_type' => 'vehicle',
            'description' => 'Technical data item',
            'category' => 'Performance',
        ]);

        $path = $this->createCsv([
            ['TEST-002', 'New interior item', 'New exterior item'],
        ]);

        $import = new YammaYapEquipmentImport(',');
        Excel::import($import, $path);

        $this->assertEquals(3, $import->deleted);
        $this->assertEquals(2, $import->imported);

        // Old items removed
        $this->assertDatabaseMissing('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Old interior item',
        ]);
        $this->assertDatabaseMissing('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Old exterior item',
        ]);
        $this->assertDatabaseMissing('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Uncategorised item',
        ]);

        // New items present
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'New interior item',
            'category' => 'Interior',
        ]);
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'New exterior item',
            'category' => 'Exterior',
        ]);

        // Exempt item still present
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Technical data item',
            'category' => 'Performance',
        ]);

        unlink($path);
    }

    public function testSkipsRowsWhereVehicleNotFound()
    {
        Vehicle::factory()->create(['uuid' => 'EXISTS-001']);

        $path = $this->createCsv([
            ['EXISTS-001', 'Air conditioning', 'Fog lights'],
            ['MISSING-001', 'Heated seats', 'Tow bar'],
        ]);

        $import = new YammaYapEquipmentImport(',');
        Excel::import($import, $path);

        $this->assertEquals(2, $import->imported);
        $this->assertEquals(1, $import->skippedRows);
        $this->assertEquals(['MISSING-001'], $import->skippedUuids);

        unlink($path);
    }

    public function testPipeDelimiter()
    {
        $vehicle = Vehicle::factory()->create(['uuid' => 'TEST-003']);

        $path = $this->createCsv([
            ['TEST-003', 'Heated seats|Leather steering wheel', 'LED headlights|Tow bar'],
        ]);

        $import = new YammaYapEquipmentImport('|');
        Excel::import($import, $path);

        $this->assertEquals(4, $import->imported);

        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Heated seats',
            'category' => 'Interior',
        ]);
        $this->assertDatabaseHas('vehicle_standard_equipment', [
            'vehicle_id' => $vehicle->id,
            'description' => 'Tow bar',
            'category' => 'Exterior',
        ]);

        unlink($path);
    }
}
