<?php
/**
 * Mersal Suite — fixed catalog of the three Mersal plugins.
 *
 * @package Mersal_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( class_exists( 'Mersal_Suite_Catalog' ) ) {
	return;
}

/**
 * Always show OTP, Campaign, and Notifications in switcher / strip / hub.
 */
class Mersal_Suite_Catalog {

	const ORDER = array( 'otp', 'nt', 'cm' );

	/**
	 * Static plugin definitions.
	 *
	 * @return array<string,array>
	 */
	public static function all() {
		return array(
			'otp' => array(
				'plugin_file'   => 'mersal-otp/mersal-otp.php',
				'folder'        => 'Mersal-OTP',
				'default_route' => 'otp.dash',
				'connect_route' => 'otp.connect',
				'download_url'  => 'https://mersal.it/wordpress-otp-plugin',
				'name'          => array( 'ar' => 'مرسال OTP', 'en' => 'Mersal OTP' ),
				'sub'           => array( 'ar' => 'تحقق ودخول آمن', 'en' => 'Verification & login' ),
				'icon'          => 'verified_user',
				'accent'        => '#0f7a4a',
			),
			'nt'  => array(
				'plugin_file'   => 'mersal-notifications/mersal-notifications.php',
				'folder'        => 'Mersal-Notifications',
				'default_route' => 'nt.dash',
				'connect_route' => 'nt.connect',
				'download_url'  => 'https://mersal.it/Wordpress-Plugins',
				'name'          => array( 'ar' => 'مرسال للإشعارات', 'en' => 'Mersal Notifications' ),
				'sub'           => array( 'ar' => 'إشعارات تلقائية', 'en' => 'Auto notifications' ),
				'icon'          => 'notifications',
				'accent'        => '#2f86c9',
			),
			'cm'  => array(
				'plugin_file'   => 'mersal-campaign/mersal-campaign.php',
				'folder'        => 'Mersal-Campaign',
				'default_route' => 'cm.dash',
				'connect_route' => 'cm.connect',
				'download_url'  => 'https://mersal.it/Wordpress-Plugins',
				'name'          => array( 'ar' => 'مرسال للحملات', 'en' => 'Mersal Campaigns' ),
				'sub'           => array( 'ar' => 'تسويق وحملات', 'en' => 'Marketing campaigns' ),
				'icon'          => 'campaign',
				'accent'        => '#ea7c39',
			),
		);
	}

	/**
	 * Catalog entry merged with live Suite registration when available.
	 *
	 * @param string           $id    Plugin id.
	 * @param Mersal_Suite|null $suite Suite instance.
	 * @return array|null
	 */
	public static function meta( $id, $suite = null ) {
		$all = self::all();
		if ( ! isset( $all[ $id ] ) ) {
			return null;
		}
		$meta = $all[ $id ];
		if ( $suite && isset( $suite->plugins()[ $id ] ) ) {
			$p = $suite->plugins()[ $id ];
			foreach ( array( 'name', 'sub', 'icon', 'accent', 'default_route' ) as $k ) {
				if ( ! empty( $p[ $k ] ) ) {
					$meta[ $k ] = $p[ $k ];
				}
			}
		}
		return $meta;
	}

	/**
	 * @param string $id Plugin id.
	 * @return bool
	 */
	public static function is_installed( $id ) {
		$def = self::meta( $id );
		if ( ! $def ) {
			return false;
		}
		$file = WP_PLUGIN_DIR . '/' . $def['plugin_file'];
		if ( file_exists( $file ) ) {
			return true;
		}
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		return is_plugin_active( $def['plugin_file'] );
	}

	/**
	 * @param string $id Plugin id.
	 * @return bool
	 */
	public static function is_active( $id ) {
		if ( ! self::is_installed( $id ) ) {
			return false;
		}
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		$def = self::meta( $id );
		return $def && is_plugin_active( $def['plugin_file'] );
	}

	/**
	 * Open dashboard route, activation link, or external download.
	 *
	 * @param string $id Plugin id.
	 * @return string
	 */
	public static function resolve_url( $id ) {
		return self::install_or_download_url( $id );
	}

	/**
	 * Activate locally or link to download page.
	 *
	 * @param string $id Plugin id.
	 * @return string
	 */
	public static function install_or_download_url( $id ) {
		$def = self::meta( $id );
		if ( ! $def ) {
			return '#';
		}
		if ( self::is_active( $id ) && class_exists( 'Mersal_Suite' ) ) {
			return Mersal_Suite::instance()->url( $def['default_route'] );
		}
		if ( self::is_installed( $id ) ) {
			if ( ! function_exists( 'activate_plugin' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			return wp_nonce_url(
				admin_url( 'plugins.php?action=activate&plugin=' . rawurlencode( $def['plugin_file'] ) ),
				'activate-plugin_' . $def['plugin_file']
			);
		}
		return $def['download_url'];
	}

	/**
	 * Status label for UI badges.
	 *
	 * @param string $id Plugin id.
	 * @param bool   $ar Arabic flag.
	 * @return string active|install|download
	 */
	public static function status( $id, $ar = true ) {
		if ( self::is_active( $id ) ) {
			return 'active';
		}
		if ( self::is_installed( $id ) ) {
			return 'install';
		}
		return 'download';
	}

	/**
	 * Human label for status badge.
	 *
	 * @param string $id Plugin id.
	 * @param bool   $ar Arabic flag.
	 * @return string
	 */
	public static function status_label( $id, $ar = true ) {
		switch ( self::status( $id, $ar ) ) {
			case 'active':
				return $ar ? 'مُثبّتة' : 'Installed';
			case 'install':
				return $ar ? 'تفعيل' : 'Activate';
			default:
				return $ar ? 'تثبيت' : 'Install';
		}
	}
}
