<?php

namespace Database\Seeders\Tenant;

use App\Facades\Settings;
use Illuminate\Database\Seeder;

/**
 * VSS-28: the shared-password field was required whenever restricted access was
 * enabled, regardless of the access mode. It is only relevant in "password" mode -
 * in "email-request" mode it must be optional. Re-point its validation at the mode
 * setting, mirroring site-access-whitelisted-domains. Existing tenants keep their
 * stored password value (withValue preserves it).
 */
class FixSiteAccessPasswordValidationSeed extends Seeder
{
    public function run(): void
    {
        collect($this->data())
            ->each(function ($setting) {
                $setting['value'] = Settings::get($setting['config_key']);
                return Settings::make($setting, true);
            });
    }

    private function data(): array
    {
        return [
            [
                'tab' => 'Site',
                'section' => 'General',
                'group' => 'Restricted access',
                'name' => 'Password',
                'config_key' => 'site-access-password',
                'type' => 'secret',
                'validation_rules' => [
                    'required_if:values.site-access-mode,password',
                ],
            ],
        ];
    }
}
