<?php
/**
 * Free-core license compatibility shim.
 *
 * Older core builds referenced TSM_License from several admin screens. The
 * public core no longer gates record creation and does not contact a license
 * server; this class keeps those references harmless and local.
 *
 * @package TradeSchoolManager
 */

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

class TSM_License {

	const OPT_KEY        = 'tsm_license_key';
	const OPT_STATUS     = 'tsm_license_status';
	const OPT_EXPIRES    = 'tsm_license_expires';
	const OPT_LAST_CHECK = 'tsm_license_last_check';
	const OPT_INSTANCE   = 'tsm_license_instance';
	const OPT_SERVER     = 'tsm_license_server';
	const OPT_MESSAGE    = 'tsm_license_message';
	const CRON_HOOK      = 'tsm_license_daily_check';

	public static function init() {
		$ts = wp_next_scheduled( self::CRON_HOOK );
		if ( $ts ) {
			wp_unschedule_event( $ts, self::CRON_HOOK );
		}
	}

	public static function is_creation_locked() {
		return false;
	}

	public static function render_lock_notice( $context = '' ) {
		return;
	}

	public static function block_or_die( $context = '', $redirect_slug = '' ) {
		return;
	}

	public static function get_key()      { return ''; }
	public static function get_status()   { return 'inactive'; }
	public static function get_expires()  { return ''; }
	public static function get_message()  { return ''; }
	public static function is_active()    { return false; }
	public static function get_server()   { return ''; }

	public static function get_instance_id() {
		$id = (string) get_option( self::OPT_INSTANCE, '' );
		if ( '' === $id ) {
			$id = wp_generate_uuid4();
			update_option( self::OPT_INSTANCE, $id, false );
		}
		return $id;
	}

	public static function activate( $key ) {
		return array(
			'ok'      => false,
			'message' => __( 'The free core does not use license activation. Install and license paid add-ons separately if needed.', 'tradeschool-manager' ),
		);
	}

	public static function deactivate() {
		self::delete_legacy_options();
		return array(
			'ok'      => true,
			'message' => __( 'Legacy license data removed.', 'tradeschool-manager' ),
		);
	}

	public static function validate() {
		return array(
			'ok'      => false,
			'message' => __( 'The free core does not require license validation.', 'tradeschool-manager' ),
			'status'  => 'inactive',
		);
	}

	public static function daily_check() {
		return self::validate();
	}

	public static function handle_form() {
		if ( ! isset( $_POST['tsm_license_action'] ) ) { return; }
		if ( ! current_user_can( 'manage_options' ) ) { return; }
		check_admin_referer( 'tsm_license' );
		self::delete_legacy_options();
		set_transient( 'tsm_license_flash', self::deactivate(), 30 );
		wp_safe_redirect( admin_url( 'admin.php?page=tsm-settings&tab=addons' ) );
		exit;
	}

	public static function maybe_show_notice() {
		return;
	}

	public static function render_panel() {
		?>
		<div class="card" style="max-width:760px;padding:20px;">
			<h2 style="margin-top:0;">
				<?php esc_html_e( 'TradeSchool Manager Free Core', 'tradeschool-manager' ); ?>
				<span style="display:inline-block;margin-left:8px;padding:3px 10px;border-radius:999px;background:#16a34a;color:#fff;font-weight:600;text-transform:uppercase;font-size:11px;letter-spacing:.5px;vertical-align:middle;">
					<?php esc_html_e( 'Free', 'tradeschool-manager' ); ?>
				</span>
			</h2>
			<p><?php esc_html_e( 'The core plugin does not require a license key and does not perform license checks. Optional paid add-ons are installed as separate plugins.', 'tradeschool-manager' ); ?></p>
			<p>
				<a class="button button-primary" target="_blank" rel="noopener" href="https://tradeschoolmanager.com/add-ons/">
					<?php esc_html_e( 'Browse Add-ons', 'tradeschool-manager' ); ?>
				</a>
				<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=tsm-settings&tab=addons' ) ); ?>">
					<?php esc_html_e( 'Add-ons in this site', 'tradeschool-manager' ); ?>
				</a>
			</p>
			<?php self::render_legacy_cleanup(); ?>
		</div>
		<?php
	}

	public static function render_legacy_cleanup() {
		if ( ! self::has_legacy_options() ) {
			return;
		}
		?>
		<hr>
		<p><?php esc_html_e( 'Legacy license settings from an older build were found. They are no longer used by the free core.', 'tradeschool-manager' ); ?></p>
		<form method="post">
			<?php wp_nonce_field( 'tsm_license' ); ?>
			<input type="hidden" name="tsm_license_action" value="cleanup">
			<button type="submit" class="button"><?php esc_html_e( 'Remove Legacy License Data', 'tradeschool-manager' ); ?></button>
		</form>
		<?php
	}

	public static function render_page() {
		echo '<div class="wrap"><h1>' . esc_html__( 'TradeSchool Manager License', 'tradeschool-manager' ) . '</h1>';
		self::render_panel();
		echo '</div>';
	}

	public static function has_legacy_options() {
		foreach ( self::legacy_options() as $opt ) {
			if ( false !== get_option( $opt, false ) ) {
				return true;
			}
		}
		return false;
	}

	private static function delete_legacy_options() {
		foreach ( self::legacy_options() as $opt ) {
			delete_option( $opt );
		}
		$ts = wp_next_scheduled( self::CRON_HOOK );
		if ( $ts ) {
			wp_unschedule_event( $ts, self::CRON_HOOK );
		}
	}

	private static function legacy_options() {
		return array(
			self::OPT_KEY,
			self::OPT_STATUS,
			self::OPT_EXPIRES,
			self::OPT_LAST_CHECK,
			self::OPT_SERVER,
			self::OPT_MESSAGE,
		);
	}
}

TSM_License::init();
