<?php
/**
 * Handles form data and saving for PowerWidget Products tab.
 *
 * @package Ilabs\Firevert\Modules\PowerWidget\Services\Form
 */

declare( strict_types=1 );

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

use Ilabs\Firevert\Core\Form\Validation\FormValidatorInterface;
use Ilabs\Firevert\Core\Settings\Form\DefaultSettingsProviderInterface;
use Ilabs\Firevert\Modules\PowerWidget\Services\Form\Validator\ProductsFormValidator;

/**
 * ProductsFormHandler class.
 */
class ProductsFormHandler extends AbstractFormHandler implements DefaultSettingsProviderInterface {

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

	/**
	 * Fields and types used in this form.
	 *
	 * @var array<string, string>
	 */
	protected array $fields = array(
		'firevert_power_widget_products_enabled'        => 'bool',
		'firevert_power_widget_products_selection_mode' => 'text',
		'firevert_power_widget_products_categories'     => 'array',
		'firevert_power_widget_products_selected'       => 'array',
		'firevert_power_widget_products_excluded_categories' => 'array',
		'firevert_power_widget_products_excluded'       => 'array',
		'firevert_power_widget_products_show_button'    => 'bool',
		'firevert_power_widget_products_button_text'    => 'text',
		'firevert_power_widget_products_button_url'     => 'text',
	);

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

	/**
	 * Returns sanitized form data for view rendering.
	 *
	 * @return array<string, mixed>
	 */
	public function get_data(): array {
		$data = $this->get_all_field_data();

		$data['firevert_power_widget_products_selected'] = is_array( $data['firevert_power_widget_products_selected'] )
			? $data['firevert_power_widget_products_selected']
			: array();

		$data['firevert_power_widget_products_excluded'] = is_array( $data['firevert_power_widget_products_excluded'] )
			? $data['firevert_power_widget_products_excluded']
			: array();

		$data['firevert_power_widget_products_categories'] = is_array( $data['firevert_power_widget_products_categories'] )
			? $data['firevert_power_widget_products_categories']
			: array();

		$data['firevert_power_widget_products_excluded_categories'] = is_array( $data['firevert_power_widget_products_excluded_categories'] )
			? $data['firevert_power_widget_products_excluded_categories']
			: array();

		return $data;
	}

	/**
	 * Returns default settings data.
	 *
	 * @return array<string, mixed>
	 */
	public function get_default_settings(): array {
		return array(
			'firevert_power_widget_products_enabled'     => true,
			'firevert_power_widget_products_selection_mode' => 'sale',
			'firevert_power_widget_products_categories'  => array(),
			'firevert_power_widget_products_selected'    => array(),
			'firevert_power_widget_products_excluded_categories' => array(),
			'firevert_power_widget_products_excluded'    => array(),
			'firevert_power_widget_products_show_button' => true,
			'firevert_power_widget_products_button_text' => __( 'View all', 'firevert' ),
			'firevert_power_widget_products_button_url'  => $this->prepare_shop_url(),
		);
	}

	/**
	 * Returns section form validator.
	 *
	 * @return FormValidatorInterface
	 */
	protected function get_validator(): FormValidatorInterface {
		return new ProductsFormValidator( $this->fields );
	}

	/**
	 * Returns active shop url or #
	 *
	 * @return string
	 */
	public function prepare_shop_url(): string {
		$shop_page_id = get_option( 'woocommerce_shop_page_id' );

		return $shop_page_id ? get_permalink( $shop_page_id ) : '#';
	}
}
