<?php

namespace Ugcify\License;

defined( 'ABSPATH' ) || exit;

/**
 * Utility functions for UGC Commerce licensing.
 *
 * @since 1.0.0
 * @package Ugcify\License
 */
class ProFunctions {

	/**
	 * Mask a license key for safe display (shows first 4 and last 4 chars).
	 *
	 * @param string $key Raw license key.
	 * @return string Masked key.
	 */
	public static function encrypt_key( $key ) {
		if ( empty( $key ) ) {
			return '';
		}

		$key_length = strlen( $key );
		if ( $key_length <= 8 ) {
			return str_repeat( '*', $key_length );
		}

		return substr( $key, 0, 4 ) . str_repeat( '*', $key_length - 8 ) . substr( $key, -4 );
	}

	/**
	 * Resolve a potentially masked key back to the real stored key.
	 *
	 * When the key submitted from the form still contains asterisks it means
	 * the UI displayed the masked value — in that case return the stored key.
	 *
	 * @param string $key Submitted key (may be masked).
	 * @return string Raw license key.
	 */
	public static function decrypt_key( $key ) {
		if ( strpos( $key, '*' ) !== false ) {
			return get_option( 'ugcify_pro_license_key', '' );
		}
		return $key;
	}

	/**
	 * Sanitize a license key string.
	 *
	 * @param string $key Raw input.
	 * @return string Sanitized key.
	 */
	public static function sanitize_license_key( $key ) {
		return sanitize_text_field( trim( $key ) );
	}

	/**
	 * Validate that a license key string meets minimum format requirements.
	 *
	 * @param string $key License key.
	 * @return bool
	 */
	public static function is_valid_license_format( $key ) {
		return ! empty( $key ) && strlen( $key ) >= 10;
	}

	/**
	 * Format a license date for display using the site's configured date format.
	 *
	 * @param string|int $date Date string or Unix timestamp.
	 * @return string Formatted date or 'N/A'.
	 */
	public static function format_license_date( $date ) {
		if ( empty( $date ) ) {
			return __( 'N/A', 'ugcify' );
		}
		$timestamp = is_numeric( $date ) ? (int) $date : strtotime( $date );
		return date_i18n( get_option( 'date_format' ), $timestamp );
	}

	/**
	 * Check if a license end date has passed.
	 *
	 * @param string|int $end_date
	 * @return bool True when expired.
	 */
	public static function is_license_expired( $end_date ) {
		if ( empty( $end_date ) ) {
			return true;
		}
		$end_timestamp = is_numeric( $end_date ) ? (int) $end_date : strtotime( $end_date );
		return $end_timestamp < time();
	}

	/**
	 * Get the number of days remaining until a license expires.
	 *
	 * @param string|int $end_date
	 * @return int Days remaining (0 if already expired or no date set).
	 */
	public static function get_license_remaining_days( $end_date ) {
		if ( empty( $end_date ) ) {
			return 0;
		}
		$end_timestamp = is_numeric( $end_date ) ? (int) $end_date : strtotime( $end_date );
		$diff          = $end_timestamp - time();
		return $diff > 0 ? (int) ceil( $diff / DAY_IN_SECONDS ) : 0;
	}

	/**
	 * Build a status badge HTML string for admin display.
	 *
	 * @param string $status License status value.
	 * @return string HTML badge.
	 */
	public static function get_license_status_badge( $status ) {
		switch ( $status ) {
			case 'active':
				$class = 'badge badge-success';
				$text  = __( 'Active', 'ugcify' );
				break;
			case 'expired':
				$class = 'badge badge-warning';
				$text  = __( 'Expired', 'ugcify' );
				break;
			case 'deactivate':
			default:
				$class = 'badge badge-danger';
				$text  = __( 'Inactive', 'ugcify' );
				break;
		}

		return sprintf( '<span class="%s">%s</span>', esc_attr( $class ), esc_html( $text ) );
	}
}
