<?php

namespace Database\Seeders\Tenant;

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

class AutoTraderHubExportFieldSettingsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * Refreshes the existing "include" whitelist choices (so newly-optional fields
     * become selectable) and adds the alternate "exclude" setting. Both derive their
     * choices from AutoTraderHub::OPTIONAL_EXPORT_FIELDS so there is a single source
     * of truth. Settings::make() updates choices without clobbering saved values.
     *
     * @return void
     */
    public function run()
    {
        $choices = [];
        foreach (AutoTraderHub::OPTIONAL_EXPORT_FIELDS as $value => $name) {
            $choices[] = ['value' => $value, 'name' => $name];
        }

        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Autotrader',
            'group' => 'Autotrader Hub',
            'name' => 'Optional fields to export',
            'config_key' => 'auto-trader-hub-exported-optional-fields',
            'type' => 'list',
            'choices' => $choices,
            'value' => null,
            'description' => 'Select which optional fields are included when vehicles are exported to '
                . 'Autotrader Hub. Unselected fields will not be sent, so clients managing those fields '
                . 'directly in Autotrader Hub will not have them overwritten. Mandatory fields are always '
                . 'exported. Leave unconfigured to export all fields (default behaviour).',
            'order' => 50,
        ]);

        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Autotrader',
            'group' => 'Autotrader Hub',
            'name' => 'Fields to exclude from export',
            'config_key' => 'auto-trader-hub-excluded-optional-fields',
            'type' => 'list',
            'choices' => $choices,
            'value' => null,
            'description' => 'Select fields that should NOT be sent to Autotrader Hub, so clients '
                . 'managing them directly in Hub are not overwritten. This only applies when "Optional '
                . 'fields to export" is left empty - if you have selected specific fields to include, '
                . 'this exclude list is ignored. Mandatory fields are always exported.',
            'order' => 51,
        ]);
    }
}
