<?php
/**
 * Handles form data and saving for the PromoTimeWidget "Widget Settings" section.
 *
 * @package Ilabs\Firevert\Modules\PromoTimeWidget\Services\Form
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\PromoTimeWidget\Services\Form;

use Ilabs\Firevert\Core\Settings\Form\DefaultSettingsProviderInterface;
use Ilabs\Firevert\Modules\PromoTimeWidget\Services\Type\PositionType;

/**
 * Class SettingsFormHandler
 *
 * Handles sanitization and data access for PromoTimeWidget settings form.
 */
class SettingsFormHandler extends AbstractFormHandler implements DefaultSettingsProviderInterface {

	/**
	 * Current section slug.
	 *
	 * @var string
	 */
	protected string $section_slug = 'settings';

	/**
	 * Fields and their expected types.
	 *
	 * @var array<string, string>
	 */
	protected array $fields = array(
		'firevert_promo_time_widget_name'               => 'text',
		'firevert_promo_time_widget_coupon'             => 'text',
		'firevert_promo_time_widget_visible_admin_only' => 'bool',
		'firevert_promo_time_widget_display_position'   => 'text',
		'firevert_promo_time_widget_display_counter'    => 'bool',
		'firevert_promo_time_widget_display_start'      => 'text',
		'firevert_promo_time_widget_display_end'        => 'text',
	);

	/**
	 * Returns translatable fields used in this form.
	 *
	 * @return array<string>
	 */
	public function get_translatable_fields(): array {
		return array(
			'firevert_promo_time_widget_name',
		);
	}

	/**
	 * Returns data to render in the settings view.
	 *
	 * @return array<string, mixed> Array of field values and post object.
	 */
	public function get_data(): array {
		return array_merge(
			array( 'post' => $this->post ),
			$this->get_all_field_data()
		);
	}

	/**
	 * Returns default settings data.
	 *
	 * @return array<string, mixed>
	 */
	public function get_default_settings(): array {
		$now       = current_time( 'Y-m-d\TH:i' );
		$timestamp = time();
		$end_time  = gmdate( 'Y-m-d\TH:i', $timestamp + WEEK_IN_SECONDS );

		return array(
			'firevert_promo_time_widget_name'             => 'Default Promo Time Widget',
			'firevert_promo_time_widget_coupon'           => 'TESTCODE',
			'firevert_promo_time_widget_visible_admin_only' => true,
			'firevert_promo_time_widget_display_position' => PositionType::AFTER_ADD_TO_CART_BUTTON,
			'firevert_promo_time_widget_display_counter'  => true,
			'firevert_promo_time_widget_display_start'    => $now,
			'firevert_promo_time_widget_display_end'      => $end_time,
		);
	}
}
