<?php

namespace Tests\Tenant;

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

class SettingAuditExclusionTest extends TenantTestCase
{
    public function testOrderOnlyChangeOnOrderableSettingIsNotAudited()
    {
        $setting = Setting::factory()->create(['group' => 'Filtering', 'order' => 0]);

        $setting->order = 5;
        $setting->setAuditEvent('updated');

        $this->assertFalse(
            $setting->readyForAuditing(),
            'An order-only change to an orderable setting should not be audited.'
        );
    }

    public function testValueChangeOnOrderableSettingIsAuditedWithoutOrder()
    {
        $setting = Setting::factory()->create([
            'group' => 'Sorting',
            'type' => 'string',
            'value' => 'old',
            'order' => 0,
        ]);

        $setting->value = 'new';
        $setting->order = 5;
        $setting->setAuditEvent('updated');

        $this->assertTrue(
            $setting->readyForAuditing(),
            'A value change should still be audited even when order also changed.'
        );
        $this->assertContains(
            'order',
            $setting->getAuditExclude(),
            'The order column must be excluded from the audit values for orderable settings.'
        );
    }

    public function testOrderChangeOnNonOrderableSettingIsStillAudited()
    {
        $setting = Setting::factory()->create(['group' => 'SomethingElse', 'order' => 0]);

        $setting->order = 5;
        $setting->setAuditEvent('updated');

        $this->assertTrue(
            $setting->readyForAuditing(),
            'Non-orderable settings keep their existing audit behaviour.'
        );
        $this->assertNotContains('order', $setting->getAuditExclude());
    }

    public function testIsOrderableSettingMatchesConfiguredGroups()
    {
        config(['automotive.orderable_setting_groups' => ['Filtering', 'Sorting']]);

        $this->assertTrue(Setting::factory()->make(['group' => 'Filtering'])->isOrderableSetting());
        $this->assertTrue(Setting::factory()->make(['group' => 'Sorting'])->isOrderableSetting());
        $this->assertFalse(Setting::factory()->make(['group' => 'General'])->isOrderableSetting());
    }
}
