<?php

declare(strict_types=1);

namespace Tests\Tenant;

use Mtc\MercuryDataModels\Setting;
use Tests\TenantTestCase;

class RenameAutoTraderToAutotraderInSettingsTest extends TenantTestCase
{
    public function testRewritesAllFourUserVisibleColumns(): void
    {
        $brandedId = Setting::create([
            'tab' => 'Automotive',
            'section' => 'AutoTrader',
            'group' => 'Stock push from website to AutoTrader',
            'name' => 'Remove vehicles from AutoTrader when sold',
            'config_key' => 'auto-trader-rename-test-1',
            'type' => 'boolean',
            'value' => false,
            'description' => 'Sends data to AutoTrader Hub.',
        ])->id;

        $unrelatedId = Setting::create([
            'tab' => 'Automotive',
            'section' => 'CodeWeavers',
            'group' => 'General',
            'name' => 'Enabled',
            'config_key' => 'auto-trader-rename-test-2',
            'type' => 'boolean',
            'value' => false,
            'description' => 'CodeWeavers description.',
        ])->id;

        $this->artisan('autonomy:rename-autotrader-settings')->assertExitCode(0);

        $branded = Setting::query()->find($brandedId);
        $this->assertSame('Autotrader', $branded->section);
        $this->assertSame('Stock push from website to Autotrader', $branded->group);
        $this->assertSame('Remove vehicles from Autotrader when sold', $branded->name);
        $this->assertSame('Sends data to Autotrader Hub.', $branded->description);

        $unrelated = Setting::query()->find($unrelatedId);
        $this->assertSame('CodeWeavers', $unrelated->section);
        $this->assertSame('General', $unrelated->group);
        $this->assertSame('Enabled', $unrelated->name);
        $this->assertSame('CodeWeavers description.', $unrelated->description);
    }

    public function testIsIdempotent(): void
    {
        Setting::create([
            'tab' => 'Automotive',
            'section' => 'AutoTrader',
            'group' => 'Stock push from website to AutoTrader',
            'name' => 'Enabled',
            'config_key' => 'auto-trader-rename-test-idempotent',
            'type' => 'boolean',
            'value' => false,
        ]);

        $this->artisan('autonomy:rename-autotrader-settings')->assertExitCode(0);
        $this->artisan('autonomy:rename-autotrader-settings')->assertExitCode(0);

        $setting = Setting::query()->where('config_key', 'auto-trader-rename-test-idempotent')->first();
        $this->assertSame('Autotrader', $setting->section);
        $this->assertSame('Stock push from website to Autotrader', $setting->group);
    }
}
