<?php

namespace Tests\Tenant;

use App\Jobs\RunImportConditionJob;
use App\Models\ImportCondition;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleAttribute;
use Mtc\MercuryDataModels\VehicleAttributeValue;
use Tests\TenantTestCase;

class RunImportConditionJobTest extends TenantTestCase
{
    public function testDoNotImportRemovesMatchingVehicles()
    {
        $this->createContactNameAttribute();
        $blacklisted = $this->vehicleWithContactName('Bad Dealer');
        $allowed = $this->vehicleWithContactName('Good Dealer');

        $condition = ImportCondition::query()->create([
            'active' => true,
            'do_not_import' => true,
        ]);
        $condition->rules()->create([
            'field' => 'attribute.contact_name',
            'condition' => 'in',
            'value' => 'Bad Dealer',
        ]);

        (new RunImportConditionJob($condition->id))->handle();

        $this->assertSoftDeleted('vehicles', ['id' => $blacklisted->id]);
        $this->assertNotSoftDeleted('vehicles', ['id' => $allowed->id]);
    }

    public function testDoNotPublishUnpublishesMatchingVehicles()
    {
        $this->createContactNameAttribute();
        $blacklisted = $this->vehicleWithContactName('Bad Dealer', ['is_published' => true]);
        $allowed = $this->vehicleWithContactName('Good Dealer', ['is_published' => true]);

        $condition = ImportCondition::query()->create([
            'active' => true,
            'do_not_publish' => true,
        ]);
        $condition->rules()->create([
            'field' => 'attribute.contact_name',
            'condition' => 'in',
            'value' => 'Bad Dealer',
        ]);

        (new RunImportConditionJob($condition->id))->handle();

        $this->assertFalse($blacklisted->fresh()->is_published);
        $this->assertTrue($allowed->fresh()->is_published);
    }

    public function testConditionWithoutRulesDoesNothing()
    {
        $vehicle = Vehicle::factory()->create();
        $condition = ImportCondition::query()->create([
            'active' => true,
            'do_not_import' => true,
        ]);

        (new RunImportConditionJob($condition->id))->handle();

        $this->assertNotSoftDeleted('vehicles', ['id' => $vehicle->id]);
    }

    private function createContactNameAttribute(): void
    {
        VehicleAttribute::factory()->create([
            'name' => 'Contact Name',
            'slug' => 'contact_name',
            'type' => 'text',
            'model' => 'vehicle',
        ]);
    }

    private function vehicleWithContactName(string $name, array $attributes = []): Vehicle
    {
        $vehicle = Vehicle::factory()->create($attributes);
        $attributeId = VehicleAttribute::query()
            ->where('model', 'vehicle')
            ->where('slug', 'contact_name')
            ->value('id');

        VehicleAttributeValue::query()->create([
            'owner_type' => 'vehicle',
            'owner_id' => $vehicle->id,
            'attribute_id' => $attributeId,
            'slug' => 'contact_name',
            'type' => 'text',
            'value_text' => $name,
            'value' => $name,
            'index' => 0,
        ]);

        return $vehicle;
    }
}
