<?php

namespace Modules\Vrc\Tests\Unit;

use Illuminate\Database\Capsule\Manager as Capsule;
use Modules\Vrc\Entities\VrcVehicle;
use PHPUnit\Framework\TestCase;

/**
 * Verifies the §6.1 acceptance "search by plate/VIN returns it" and the
 * vehicle label, against a self-contained in-memory SQLite DB (no app boot,
 * no live database touched). SoftDeletes is disabled for the bare schema.
 */
class VehicleSearchTest extends TestCase
{
    private Capsule $capsule;

    protected function setUp(): void
    {
        parent::setUp();

        $this->capsule = new Capsule;
        $this->capsule->addConnection([
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ]);
        $this->capsule->setAsGlobal();
        $this->capsule->bootEloquent();

        Capsule::schema()->create('vrc_vehicles', function ($t) {
            $t->increments('id');
            $t->integer('business_id');
            $t->integer('erp_customer_id');
            $t->integer('make_id')->nullable();
            $t->integer('model_id')->nullable();
            $t->integer('year')->nullable();
            $t->string('plate_number')->nullable();
            $t->string('vin')->nullable();
            $t->string('engine_number')->nullable();
            $t->string('color')->nullable();
            $t->string('transmission')->nullable();
            $t->string('fuel_type')->nullable();
            $t->integer('odometer')->nullable();
            $t->string('odometer_unit')->default('km');
            $t->string('vehicle_type')->nullable();
            $t->date('registration_expiry')->nullable();
            $t->integer('insurance_company_id')->nullable();
            $t->string('insurance_policy_no')->nullable();
            $t->date('insurance_expiry')->nullable();
            $t->text('notes')->nullable();
            $t->boolean('is_active')->default(true);
            $t->timestamps();
            $t->timestamp('deleted_at')->nullable();
        });

        VrcVehicle::query()->insert([
            ['business_id' => 1, 'erp_customer_id' => 10, 'plate_number' => 'ABC-123', 'vin' => 'JT1234567890', 'engine_number' => 'ENG-9', 'odometer_unit' => 'km', 'is_active' => 1],
            ['business_id' => 1, 'erp_customer_id' => 11, 'plate_number' => 'XYZ-999', 'vin' => 'WVW000111222', 'engine_number' => 'ENG-1', 'odometer_unit' => 'km', 'is_active' => 1],
            ['business_id' => 2, 'erp_customer_id' => 12, 'plate_number' => 'ABC-123', 'vin' => 'OTHER-BIZ', 'engine_number' => null, 'odometer_unit' => 'km', 'is_active' => 1],
        ]);
    }

    protected function tearDown(): void
    {
        Capsule::schema()->drop('vrc_vehicles');
        parent::tearDown();
    }

    public function test_search_by_plate_returns_match_scoped_to_business(): void
    {
        $results = VrcVehicle::where('business_id', 1)->search('ABC-123')->get();
        $this->assertCount(1, $results);
        $this->assertSame('JT1234567890', $results->first()->vin);
    }

    public function test_search_by_vin(): void
    {
        $results = VrcVehicle::where('business_id', 1)->search('WVW000')->get();
        $this->assertCount(1, $results);
        $this->assertSame('XYZ-999', $results->first()->plate_number);
    }

    public function test_search_by_engine_number(): void
    {
        $results = VrcVehicle::where('business_id', 1)->search('ENG-9')->get();
        $this->assertCount(1, $results);
        $this->assertSame('ABC-123', $results->first()->plate_number);
    }

    public function test_empty_search_returns_all_for_business(): void
    {
        $results = VrcVehicle::where('business_id', 1)->search('')->get();
        $this->assertCount(2, $results);
    }

    public function test_label_uses_plate_when_present(): void
    {
        $v = VrcVehicle::where('business_id', 1)->where('plate_number', 'ABC-123')->first();
        $this->assertStringContainsString('ABC-123', $v->label);
    }
}
