<?php

namespace Tests\Tenant;

use App\Console\Commands\CleanupDuplicateMotorsDealerships;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class CleanupDuplicateMotorsDealershipsTest extends TenantTestCase
{
    public function testNoDuplicatesReportsCleanly()
    {
        Dealership::factory()->create([
            'data' => ['motors-location-id' => 'LOC-1'],
        ]);

        $this->artisan(CleanupDuplicateMotorsDealerships::class)
            ->expectsOutputToContain('No duplicate dealerships found.')
            ->assertSuccessful();
    }

    public function testMergesDuplicatesAndReassignsVehicles()
    {
        $canonical = Dealership::factory()->create([
            'name' => 'Canonical Dealer',
            'data' => ['motors-location-id' => 'LOC-DUP'],
        ]);
        $duplicateOne = Dealership::factory()->create([
            'name' => 'Duplicate One',
            'data' => ['motors-location-id' => 'LOC-DUP'],
        ]);
        $duplicateTwo = Dealership::factory()->create([
            'name' => 'Duplicate Two',
            'data' => ['motors-location-id' => 'LOC-DUP'],
        ]);

        Vehicle::factory()->create([
            'dealership_id' => $canonical->id,
            'stock_provider' => 'motors',
            'uuid' => 'V-1',
        ]);
        Vehicle::factory()->create([
            'dealership_id' => $duplicateOne->id,
            'stock_provider' => 'motors',
            'uuid' => 'V-2',
        ]);
        Vehicle::factory()->create([
            'dealership_id' => $duplicateTwo->id,
            'stock_provider' => 'motors',
            'uuid' => 'V-3',
        ]);

        $this->artisan(CleanupDuplicateMotorsDealerships::class)->assertSuccessful();

        $this->assertEquals(
            1,
            Dealership::query()->where('data->motors-location-id', 'LOC-DUP')->count()
        );
        $this->assertEquals(
            3,
            Vehicle::query()->where('dealership_id', $canonical->id)->count()
        );
        $this->assertNull(Dealership::query()->find($duplicateOne->id));
        $this->assertNull(Dealership::query()->find($duplicateTwo->id));
    }

    public function testDryRunDoesNotWrite()
    {
        $canonical = Dealership::factory()->create([
            'data' => ['motors-location-id' => 'LOC-DRY'],
        ]);
        $duplicate = Dealership::factory()->create([
            'data' => ['motors-location-id' => 'LOC-DRY'],
        ]);

        Vehicle::factory()->create([
            'dealership_id' => $duplicate->id,
            'stock_provider' => 'motors',
            'uuid' => 'V-DRY-1',
        ]);

        $this->artisan(CleanupDuplicateMotorsDealerships::class, ['--dry-run' => true])
            ->expectsOutputToContain('[dry-run]')
            ->assertSuccessful();

        $this->assertEquals(
            2,
            Dealership::query()->where('data->motors-location-id', 'LOC-DRY')->count()
        );
        $this->assertEquals(
            $duplicate->id,
            Vehicle::query()->where('uuid', 'V-DRY-1')->first()->dealership_id
        );
    }

    public function testIgnoresDealershipsWithoutMotorsLocationId()
    {
        Dealership::factory()->create(['name' => 'Manual A', 'data' => null]);
        Dealership::factory()->create(['name' => 'Manual B', 'data' => null]);

        $this->artisan(CleanupDuplicateMotorsDealerships::class)
            ->expectsOutputToContain('No duplicate dealerships found.')
            ->assertSuccessful();

        $this->assertEquals(2, Dealership::query()->count());
    }
}
