<?php
/**
 * Utility class for logging.
 *
 * @package Replyan_AI
 */

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

final class Replyan_AI_Logs {
	/**
	 * Write a message to the logs.txt file.
	 *
	 * @param string $message The message to log.
	 */
	public static function log( string $message ): void {
		if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
			return;
		}

		$upload_dir = wp_upload_dir();
		$log_dir    = trailingslashit( $upload_dir['basedir'] ) . 'replyan-ai';

		if ( ! file_exists( $log_dir ) ) {
			wp_mkdir_p( $log_dir );
			file_put_contents( $log_dir . '/index.php', '<?php // Silence is golden.' );
		}

		$log_file = $log_dir . '/logs.txt';
		$time     = current_time( 'mysql' );
		file_put_contents( $log_file, "[{$time}] {$message}\n", FILE_APPEND );
	}
}
