<?php

declare(strict_types=1);

namespace Tests\Tenant\Listeners;

use App\Events\NewVehicleImported;
use App\Events\VehicleUpdatedFromImport;
use App\Facades\Settings;
use App\Listeners\MarkVehicleAutoTraderExportPending;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleAutoTraderData;
use Tests\TenantTestCase;

class MarkVehicleAutoTraderExportPendingTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        Settings::make([
            'tab' => 'foo',
            'section' => 'foo',
            'group' => 'foo',
            'name' => 'foo',
            'config_key' => 'sales-channels-auto-trader-enabled',
            'value' => true,
            'type' => 'boolean',
        ]);
    }

    public function testNewVehicleImportedMarksPending(): void
    {
        $vehicle = Vehicle::factory()->create();

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new NewVehicleImported($vehicle, [], 'auto-trader'));

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertNotNull($row);
        $this->assertTrue($row->pending_at_export);
    }

    public function testNewVehicleImportedReusesExistingAutoTraderRow(): void
    {
        $vehicle = Vehicle::factory()->create();
        VehicleAutoTraderData::factory()->create([
            'vehicle_id' => $vehicle->id,
            'pending_at_export' => false,
            'valuation' => 12345,
        ]);

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new NewVehicleImported($vehicle, [], 'auto-trader'));

        $this->assertSame(
            1,
            VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->count(),
        );

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertTrue($row->pending_at_export);
        $this->assertSame(12345, (int) $row->valuation);
    }

    public function testVehicleUpdatedFromImportMarksPendingWhenTrackedFieldChanges(): void
    {
        $vehicle = Vehicle::factory()->create(['price' => 10000]);
        VehicleAutoTraderData::factory()->create([
            'vehicle_id' => $vehicle->id,
            'pending_at_export' => false,
        ]);

        $vehicle->price = 12000;
        $vehicle->save();

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new VehicleUpdatedFromImport($vehicle, [], 'auto-trader'));

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertTrue($row->pending_at_export);
    }

    public function testVehicleUpdatedFromImportMarksPendingWhenIsPublishedChanges(): void
    {
        $vehicle = Vehicle::factory()->create(['is_published' => false]);
        VehicleAutoTraderData::factory()->create([
            'vehicle_id' => $vehicle->id,
            'pending_at_export' => false,
        ]);

        $vehicle->is_published = true;
        $vehicle->save();

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new VehicleUpdatedFromImport($vehicle, [], 'auto-trader'));

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertTrue($row->pending_at_export);
    }

    public function testVehicleUpdatedFromImportIgnoresUntrackedFieldChange(): void
    {
        $vehicle = Vehicle::factory()->create(['featured' => false]);
        VehicleAutoTraderData::factory()->create([
            'vehicle_id' => $vehicle->id,
            'pending_at_export' => false,
        ]);

        $vehicle->featured = true;
        $vehicle->save();

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new VehicleUpdatedFromImport($vehicle, [], 'auto-trader'));

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertFalse($row->pending_at_export);
    }

    public function testVehicleUpdatedFromImportCreatesAutoTraderRowOnFirstTrackedChange(): void
    {
        $vehicle = Vehicle::factory()->create(['price' => 10000]);

        $vehicle->price = 11000;
        $vehicle->save();

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new VehicleUpdatedFromImport($vehicle, [], 'auto-trader'));

        $row = VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first();
        $this->assertNotNull($row);
        $this->assertTrue($row->pending_at_export);
    }

    public function testDoesNotMarkPendingWhenChannelDisabled(): void
    {
        Settings::update('sales-channels-auto-trader-enabled', false);

        $vehicle = Vehicle::factory()->create(['price' => 10000]);

        $listener = $this->app->make(MarkVehicleAutoTraderExportPending::class);
        $listener->handle(new NewVehicleImported($vehicle, [], 'auto-trader'));

        $this->assertNull(
            VehicleAutoTraderData::query()->where('vehicle_id', $vehicle->id)->first(),
        );
    }
}
