<?php

namespace RadiusTheme\RadiusBooking\ExportImport\Importers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use RadiusTheme\RadiusBooking\ExportImport\Contracts\ImporterInterface;
use RadiusTheme\RadiusBooking\ExportImport\IdMapper;
use RadiusTheme\RadiusBooking\Models\Settings;

/**
 * Class SettingsImporter
 *
 * Imports plugin settings into wp_options.
 * Uses Settings::updateSettings() directly with clean keys to avoid
 * the double-suffix bug in Settings::import().
 *
 * @package RadiusTheme\RadiusBooking\ExportImport\Importers
 */
class SettingsImporter implements ImporterInterface {

	/**
	 * {@inheritDoc}
	 */
	public function getModuleName(): string {
		return 'settings';
	}

	/**
	 * {@inheritDoc}
	 */
	public function import( array $records, IdMapper $id_mapper ): int {
		if ( ! isset( $records['settings'] ) || ! is_array( $records['settings'] ) ) {
			return 0;
		}

		$settings = new Settings();
		$success  = true;

		foreach ( $records['settings'] as $key => $value ) {
			// Keys are clean (e.g. 'statuses', 'dateTime').
			// updateSettings() adds prefix 'rtrb_' and suffix '_settings' correctly.
			if ( ! $settings->updateSettings( $key, $value ) ) {
				$success = false;
			}
		}

		return $success ? 1 : 0;
	}

	/**
	 * {@inheritDoc}
	 */
	public function getDependencies(): array {
		return array();
	}
}
