<?php
/**
 * The Pro Module loader
 *
 * @since      1.0.0
 * @package    RankMath
 * @subpackage RankMathPro
 * @author     RankMath <support@rankmath.com>
 */

namespace RankMathPro;

use RankMath\Helper;
use RankMath\Helpers\Param;
use RankMath\Traits\Hooker;

defined( 'ABSPATH' ) || exit;

/**
 * Modules class.
 */
class Modules {

	use Hooker;

	/**
	 * The Constructor.
	 */
	public function __construct() {
		if ( Helper::is_heartbeat() ) {
			return;
		}

		$this->filter( 'rank_math/modules', 'setup_core', 1 );
		$this->action( 'admin_enqueue_scripts', 'enqueue' );
		$this->action( 'rank_math/module_changed', 'flush_rewrite_rules', 10 );

		// Hook for Link Genius module enablement and table recreation.
		$this->action( 'rank_math/admin/after_create_tables', 'setup_link_genius_schema', 10, 2 );
	}

	/**
	 * Setup core modules.
	 *
	 * @param array $modules Array of modules.
	 *
	 * @return array
	 */
	public function setup_core( $modules ) {
		$active_modules = get_option( 'rank_math_modules', [] );

		$modules['news-sitemap'] = [
			'title'         => esc_html__( 'News Sitemap', 'rank-math-pro' ),
			'desc'          => esc_html__( 'Create a News Sitemap for your news-related content. You only need a News sitemap if you plan on posting news-related content on your website.', 'rank-math-pro' ),
			'class'         => 'RankMathPro\Sitemap\News_Sitemap',
			'icon'          => 'post',
			'settings'      => Helper::get_settings_url( 'sitemap', 'news-sitemap' ),
			'probadge'      => defined( 'RANK_MATH_PRO_FILE' ),
			'disabled'      => ( ! in_array( 'sitemap', $active_modules, true ) ),
			'dep_modules'   => [ 'sitemap' ],
			'disabled_text' => esc_html__( 'Please activate Sitemap module to use this module.', 'rank-math-pro' ),
		];

		$modules['video-sitemap'] = [
			'title'         => esc_html__( 'Video Sitemap', 'rank-math-pro' ),
			'desc'          => esc_html__( 'For your video content, a Video Sitemap is a recommended step for better rankings and inclusion in the Video search.', 'rank-math-pro' ),
			'class'         => 'RankMathPro\Sitemap\Video_Sitemap',
			'icon'          => 'video',
			'settings'      => Helper::get_settings_url( 'sitemap', 'video-sitemap' ),
			'probadge'      => defined( 'RANK_MATH_PRO_FILE' ),
			'disabled'      => ( ! in_array( 'rich-snippet', $active_modules, true ) || ! in_array( 'sitemap', $active_modules, true ) ),
			'dep_modules'   => [ 'sitemap', 'rich-snippet' ],
			'disabled_text' => esc_html__( 'Please activate Schema & Sitemap module to use this module.', 'rank-math-pro' ),
		];

		$modules['podcast'] = [
			'title'         => esc_html__( 'Podcast', 'rank-math-pro' ),
			'desc'          => esc_html__( 'Make your podcasts discoverable via Google Podcasts, Apple Podcasts, and similar services with Podcast RSS feed and Schema Markup generated by Rank Math.', 'rank-math-pro' ),
			'class'         => 'RankMathPro\Podcast\Podcast',
			'icon'          => 'podcast',
			'betabadge'     => true,
			'settings'      => Helper::get_settings_url( 'general', 'podcast' ),
			'probadge'      => defined( 'RANK_MATH_PRO_FILE' ),
			'disabled'      => ! in_array( 'rich-snippet', $active_modules, true ),
			'dep_modules'   => [ 'rich-snippet' ],
			'disabled_text' => esc_html__( 'Please activate Schema module to use this module.', 'rank-math-pro' ),
		];

		$registered             = \RankMath\Admin\Admin_Helper::get_registration_data();
		$is_connected           = $registered && ! empty( $registered['username'] ) && ! empty( $registered['api_key'] );
		$modules['marketplace'] = [
			'title'         => esc_html__( 'Marketplace', 'rank-math-pro' ),
			'desc'          => esc_html__( 'Discover handpicked WordPress plugins designed to boost your SEO, improve Core Web Vitals, and accelerate your website\'s growth.', 'rank-math-pro' ),
			'class'         => 'RankMathPro\Marketplace\Marketplace',
			'icon'          => 'cart',
			'probadge'      => defined( 'RANK_MATH_PRO_FILE' ),
			'disabled'      => ! $is_connected,
			'disabled_text' => esc_html__( 'Please connect your site to Rank Math to use this module.', 'rank-math-pro' ),
		];

		// Replace Schema Loader.
		$modules['rich-snippet']['class'] = '\RankMathPro\Schema\Schema';

		if ( isset( $modules['link-genius'] ) ) {
			$modules['link-counter']['title'] = $modules['link-genius']['title'];
			$modules['link-counter']['desc']  = $modules['link-genius']['desc'];
			unset( $modules['link-genius'] );
		}

		return $modules;
	}

	/**
	 * Enqueue styles and scripts.
	 */
	public function enqueue() {
		if ( 'rank-math' !== Param::get( 'page' ) ) {
			return;
		}
		wp_enqueue_script( 'rank-math-pro-dashboard', RANK_MATH_PRO_URL . 'assets/admin/js/dashboard.js', [ 'jquery' ], rank_math_pro()->version, true );
	}

	/**
	 * Function to run when Module is enabled/disabled.
	 *
	 * @param string $module Module.
	 */
	public function flush_rewrite_rules( $module ) {
		if ( 'podcast' === $module ) {
			Helper::schedule_flush_rewrite();
		}
	}

	/**
	 * Set up Link Genius PRO schema when tables are created.
	 *
	 * This method is called when FREE plugin creates tables for link-counter module.
	 * It handles both module enablement and table recreation scenarios.
	 *
	 * @param array $tables Tables that were created.
	 */
	public function setup_link_genius_schema( $tables ) {
		// Only proceed if link-counter module tables were created.
		if ( ! isset( $tables['link-counter'] ) || ! isset( $tables['link-counter']['rank_math_internal_links'] ) ) {
			return;
		}

		// Initialize Link Genius PRO schema (columns, indexes, tables).
		\RankMathPro\Link_Genius\Data\Table_Extension::initialize_schema();
	}
}
