<?php
namespace Prodigy\Includes;

use Prodigy\Includes\Frontend\Pages\Prodigy_Page;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Helpers\Traits\TraitProdigySidebar;
use Prodigy\includes\models\Prodigy_Product_Attributes;

defined( 'ABSPATH' ) || exit;

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @version    2.1.0
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Activator {
	use TraitProdigySidebar;

	/** @var Prodigy_Page  */
	public Prodigy_Page $prodigy_pages;

	/** @var Prodigy_Options */
	private Prodigy_Options $options;

	/** @var Prodigy_Product_Attributes  */
	private Prodigy_Product_Attributes $product_attributes;

	/**
	 * @param Prodigy_Options            $prodigy_option
	 * @param Prodigy_Page               $prodigy_page
	 * @param Prodigy_Product_Attributes $product_attributes
	 */
	public function __construct(
		Prodigy_Options $prodigy_option,
		Prodigy_Page $prodigy_page,
		Prodigy_Product_Attributes $product_attributes
	) {
		$this->options            = $prodigy_option;
		$this->prodigy_pages      = $prodigy_page;
		$this->product_attributes = $product_attributes;
		$this->install_prodigy_pages();
		$this->install_sidebars();
		$this->set_default_tables();
		$this->set_default_options();

		update_option( 'prodigy_queue_flush_rewrite_rules', 'yes' );
	}

	/**
	 * Install shop page sidebar
	 */
	public function install_sidebars() {
		$attributes = $this->product_attributes->select_attribute_taxonomy_like_slug( Prodigy_Url_Helper::get_hs_store_subdomain() );

		$filters = array_map(
			function ( $attribute ) {
				return $attribute->id;
			},
			$attributes
		);

		$this->delete_sidebars();
		$this->set_filter_widget_settings( $filters );
	}

	/**
	 * @return void
	 */
	public function install_prodigy_pages(): void {
		$prodigy_pages = array(
			Prodigy_Page::PAGE_TYPE_SHOP  => array(
				'name'  => _x( 'shop', 'Page slug', 'prodigy-commerce' ),
				'title' => _x( 'Shop', 'Page title', 'prodigy-commerce' ),
			),
			Prodigy_Page::PAGE_TYPE_CART  => array(
				'name'    => _x( 'cart', 'Page slug', 'prodigy-commerce' ),
				'title'   => _x( 'Cart', 'Page title', 'prodigy-commerce' ),
				'content' => '[prodigy_cart] [prodigy_related_products]',
			),
			Prodigy_Page::PAGE_TYPE_THANK => array(
				'name'    => _x( 'thank-you', 'Page slug', 'prodigy-commerce' ),
				'title'   => _x( 'Thank you', 'Page title', 'prodigy-commerce' ),
				'content' => '[prodigy_thank_you_page]',
			),
		);

		$page_ids = $this->prodigy_pages->create_pages( $prodigy_pages );
		$this->prodigy_pages->prodigy_pages_update_options( $page_ids );

		if ( defined( 'PRODIGY_VERSION' ) ) {
			update_option( 'PRODIGY_VERSION', PRODIGY_VERSION );
		}
	}



	/**
	 * @return void
	 */
	public function set_default_tables(): void {
		Prodigy_Install_Wizard::set_tables();
	}

	/**
	 * Activate plugin options
	 *
	 * @return void
	 */
	public function set_default_options(): void {
		$this->options->set_default_options();
		Prodigy::customize_current_theme();
	}
}
