<?php
/**
 * Create_Default_Settings Migration Class
 *
 * @since   3.0.0
 * @package DokanKits\Setup\Migrations
 */

namespace DokanKits\Setup\Migrations\V300;

use DokanKits\Core\Data\Models\Settings;
use DokanKits\Core\Data\Stores\SettingsDataStore;
use DokanKits\Setup\Migrations\Migration;

/**
 * Class Create_Default_Settings
 *
 * Handles migration of creating default settings.
 *
 * @since 3.0.0
 */
class CreateDefaultSettings extends Migration {

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'3.0.0',
			'create_default_settings',
			'settings'
		);
	}

	/**
	 * Run the migration.
	 *
	 * @param string $version Version being migrated to.
	 *
	 * @return void
	 * @throws \Exception If migration fails.
	 */
	public function migrate( string $version ): void {
		$data_store       = dokan_kits_get_container()->get( SettingsDataStore::class );
		$default_settings = $this->get_default_settings();

		// Create settings for each category.
		foreach ( $default_settings as $category => $values ) {
			$settings = $data_store->get_settings_by_name( $category );

			if ( ! $settings instanceof Settings ) {
				$settings = dokan_kits_get_container()->get( Settings::class );
				$settings->set_name( $category );
				$settings->set_value( $values );
				$settings->set_default( $values );
				$settings->save();
			}
		}

		/**
		 * Action fired after creating default settings during migration.
		 *
		 * @since 3.0.0
		 *
		 * @param string $version Migration version.
		 */
		do_action( 'dokan_kits_after_migrate_settings', $version );
	}

	/**
	 * Get default settings for all categories.
	 *
	 * @return array Default settings.
	 */
	private function get_default_settings(): array {
		return array(
			'vendor'   => array(
				'registration' => array(
					'remove_vendor_checkbox'               => false,
					'set_default_seller_role_checkbox'     => false,
					'remove_become_a_vendor_button_checkbox' => false,
					'enable_own_product_purchase_checkbox' => false,
				),
			),
			'product'  => array(
				'types'        => array(
					'remove_simple_product_checkbox'   => false,
					'remove_variable_product_checkbox' => false,
					'remove_external_product_checkbox' => false,
					'remove_grouped_product_checkbox'  => false,
				),
				'fields'       => array(
					'remove_short_description_checkbox'   => false,
					'remove_long_description_checkbox'    => false,
					'remove_inventory_section_checkbox'   => false,
					'remove_downloadable_checkbox'        => false,
					'remove_virtual_checkbox'             => false,
					'remove_linked_product_checkbox'      => false,
					'remove_attribute_variation_checkbox' => false,
					'remove_shipping_tax_option_checkbox' => false,
					'remove_geolocation_option_checkbox'  => false,
				),
				'pro_features' => array(
					'remove_bulk_discount_checkbox'   => false,
					'remove_rma_checkbox'             => false,
					'remove_wholesale_checkbox'       => false,
					'remove_min_max_product_checkbox' => false,
					'remove_other_options_checkbox'   => false,
					'remove_product_advertisement_checkbox' => false,
				),
				'images'       => array(
					'enable_dimension_restrictions' => false,
					'enable_size_restrictions'      => false,
					'image_max_width'               => 800,
					'image_max_height'              => 800,
					'image_max_size'                => 2,
				),
			),
			'shipping' => array(
				'remove_split_shipping_checkbox'     => false,
				'remove_split_shipping_pro_checkbox' => false,
			),
			'cart'     => array(
				'hide_add_to_cart_button_checkbox' => false,
			),
		);
	}
}
