<?php
/**
 * ProQyz custom debug log class.
 *
 * Writes text log files under proqyz/logs/ by date (dd-mm-yy).
 * Only active when get_option('st_proqyz_custom_debug_log') is truthy.
 * Use for custom hooks and manual logging. Each entry starts with a blank line.
 *
 * @package ProQyz
 * @since   1.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class ST_ProQyz_Debug_Log
 */
class ST_ProQyz_Debug_Log {

	/**
	 * Option name to enable/disable custom debug logging.
	 *
	 * @var string
	 */
	const OPTION_ENABLE = 'st_proqyz_custom_debug_log';

	/**
	 * Relative logs directory name under ProQyz root.
	 *
	 * @var string
	 */
	const LOGS_DIR = 'logs';

	/**
	 * Date format for log file names: dd-mm-yy.
	 *
	 * @var string
	 */
	const FILE_DATE_FORMAT = 'd-m-y';

	/**
	 * Time format for log line prefix: hh:mm:ss.
	 *
	 * @var string
	 */
	const TIME_FORMAT = 'H:i:s';

	/**
	 * Log type constants (can also use custom strings).
	 *
	 * @var string
	 */
	const TYPE_INFO  = 'INFO';
	const TYPE_DEBUG = 'DEBUG';
	const TYPE_ERROR = 'ERROR';
	const TYPE_HOOK  = 'HOOK';

	/**
	 * Absolute path to the logs directory.
	 *
	 * @var string|null
	 */
	private static $logs_path = null;

	/**
	 * Get the absolute path to the logs directory (proqyz/logs).
	 *
	 * @return string
	 */
	public static function get_logs_path() {
		if ( self::$logs_path === null ) {
			self::$logs_path = rtrim( ST_PROQYZ_ABSPATH, '/' ) . '/' . self::LOGS_DIR . '/';
		}
		return self::$logs_path;
	}

	/**
	 * Check whether custom debug logging is enabled.
	 *
	 * @return bool
	 */
	public static function is_enabled() {
		return (bool) get_option( self::OPTION_ENABLE, true );
	}

	/**
	 * Ensure the logs directory exists. Create if missing.
	 *
	 * @return bool True if directory exists or was created, false on failure.
	 */
	private static function ensure_logs_dir() {
		$dir = self::get_logs_path();
		if ( is_dir( $dir ) ) {
			return true;
		}
		return wp_mkdir_p( $dir );
	}

	/**
	 * Get the log file path for today (dd-mm-yy.txt).
	 *
	 * @return string
	 */
	public static function get_today_log_file() {
		return self::get_logs_path() . date( self::FILE_DATE_FORMAT ) . '.txt';
	}

	/**
	 * Normalize message to string; arrays/objects as print_r output.
	 *
	 * @param mixed $message Message or variable to log.
	 * @return string
	 */
	private static function normalize_message( $message ) {
		if ( is_string( $message ) ) {
			return $message;
		}
		return print_r( $message, true );
	}

	/**
	 * Write a single log entry. Adds one blank line before the entry, then:
	 * [hh:mm:ss] [log type] - first line of message
	 * Optional multiline content (e.g. print_r) on following lines.
	 *
	 * @param string $type    Log type (e.g. INFO, DEBUG, ERROR, HOOK).
	 * @param mixed  $message Message or variable (arrays/objects printed with print_r).
	 * @return bool True if written (and logging enabled), false otherwise.
	 */
	public static function log( $type, $message ) {
		if ( ! self::is_enabled() ) {
			return false;
		}

		if ( ! self::ensure_logs_dir() ) {
			return false;
		}

		$time   = date( self::TIME_FORMAT );
		$type   = is_string( $type ) ? trim( $type ) : self::TYPE_INFO;
		$string = self::normalize_message( $message );

		// One blank line before each new log entry.
		$entry = "\n";
		$lines = explode( "\n", $string );
		$first = array_shift( $lines );
		$entry .= '[' . $time . '] [' . $type . '] - ' . $first;
		if ( ! empty( $lines ) ) {
			$entry .= "\n" . implode( "\n", $lines );
		}
		$entry .= "\n";

		$file = self::get_today_log_file();
		return ( false !== file_put_contents( $file, $entry, FILE_APPEND | LOCK_EX ) );
	}

	/**
	 * Shorthand: log with type INFO.
	 *
	 * @param mixed $message Message or variable to log.
	 * @return bool
	 */
	public static function info( $message ) {
		return self::log( self::TYPE_INFO, $message );
	}

	/**
	 * Shorthand: log with type DEBUG.
	 *
	 * @param mixed $message Message or variable to log.
	 * @return bool
	 */
	public static function debug( $message ) {
		return self::log( self::TYPE_DEBUG, $message );
	}

	/**
	 * Shorthand: log with type ERROR.
	 *
	 * @param mixed $message Message or variable to log.
	 * @return bool
	 */
	public static function error( $message ) {
		return self::log( self::TYPE_ERROR, $message );
	}

	/**
	 * Shorthand: log with type HOOK (for custom hooks).
	 *
	 * @param mixed $message Message or variable to log.
	 * @return bool
	 */
	public static function hook( $message ) {
		return self::log( self::TYPE_HOOK, $message );
	}
}
