<?php

namespace Tests\Tenant;

use Database\Seeders\Tenant\SantanderWidgetSettingSeeder;
use Illuminate\Support\Facades\Event;
use Mtc\MercuryDataModels\Events\SettingUpdated;
use Mtc\MercuryDataModels\Setting;
use Tests\TenantTestCase;

class SantanderWidgetSettingSeederTest extends TenantTestCase
{
    protected bool $useTestSettingRepository = false;

    private const CONFIG_KEYS = [
        'finance-santander-api-enabled',
        'finance-santander-widget-enabled',
        'finance-santander-widget-key',
    ];

    public function testSeedsAllThreeSantanderWidgetSettings()
    {
        (new SantanderWidgetSettingSeeder())->run();

        foreach (self::CONFIG_KEYS as $configKey) {
            $setting = Setting::query()->where('config_key', $configKey)->first();

            $this->assertNotNull($setting, "Expected {$configKey} to be seeded");
            $this->assertEquals('Automotive', $setting->tab);
            $this->assertEquals('Finance', $setting->section);
            $this->assertEquals('Santander', $setting->group);
        }
    }

    public function testApiEnabledDefaultsToTrueToPreserveExistingBehaviour()
    {
        (new SantanderWidgetSettingSeeder())->run();

        $setting = Setting::query()
            ->where('config_key', 'finance-santander-api-enabled')
            ->first();

        $this->assertEquals('boolean', $setting->type);
        $this->assertTrue((bool) $setting->value);
    }

    public function testWidgetEnabledDefaultsToFalse()
    {
        (new SantanderWidgetSettingSeeder())->run();

        $setting = Setting::query()
            ->where('config_key', 'finance-santander-widget-enabled')
            ->first();

        $this->assertEquals('boolean', $setting->type);
        $this->assertFalse((bool) $setting->value);
    }

    public function testWidgetKeyIsStoredAsSecretAndRequiredWhenWidgetEnabled()
    {
        (new SantanderWidgetSettingSeeder())->run();

        $setting = Setting::query()
            ->where('config_key', 'finance-santander-widget-key')
            ->first();

        $this->assertEquals('secret', $setting->type);
        $this->assertContains(
            'required_if:values.finance-santander-widget-enabled,true',
            $setting->validation_rules ?? [],
        );
    }

    public function testReRunningTheSeederDoesNotDuplicateSettingsOrOverwriteExistingValues()
    {
        // Settings::make on an existing key fires SettingUpdated, whose listeners
        // reach the central database; they are irrelevant to the seeder's data
        // behaviour, so suppress them here.
        Event::fake([SettingUpdated::class]);

        Setting::factory()->create([
            'tab' => 'Automotive',
            'section' => 'Finance',
            'group' => 'Santander',
            'name' => 'Santander Widget API Key',
            'config_key' => 'finance-santander-widget-key',
            'type' => 'secret',
            'value' => 'existing-key',
        ]);

        (new SantanderWidgetSettingSeeder())->run();

        $widgetKeys = Setting::query()
            ->where('config_key', 'finance-santander-widget-key')
            ->get();

        $this->assertCount(1, $widgetKeys);
        $this->assertEquals('existing-key', $widgetKeys->first()->value);
    }
}
