<?php
namespace Actirise\Includes;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

use Actirise\Includes\Options;

/**
 * Logger class.
 *
 * @link       https://actirise.com
 * @since      2.5.0
 * @package    actirise
 * @subpackage actirise/includes
 * @author     actirise <wordpress@actirise.com>
 */
class Logger {
	/**
	 * Add log.
	 *
	 * @since 2.5.0
	 * @param string $message
	 * @param string $file
	 * @param string $type
	 * @return void
	 */
	public static function add_log( $message, $file, $type ) {
		self::add_log_to_db( $message, $file, $type );
	}

	/**
	 * Add log Action.
	 *
	 * @since 2.6.9
	 * @param string $message
	 * @return void
	 */
	public static function add_log_action( $message ) {
		self::add_log_action_to_db( $message );
	}

	/**
	 * Get logs.
	 *
	 * @since 2.5.0
	 * @return array<array<'datelog'|'file'|'message'|'type', string>>
	 */
	public static function get_logs() {
		/** @var array<array<'datelog'|'file'|'message'|'type', string>> $clear_logs */
		$clear_logs = self::get_clean_logs( 'logs' );

		$clean_logs = array();

		foreach ( $clear_logs as $log ) {
			$clean_logs[] = array(
				'datelog' => $log['datelog'],
				'message' => $log['message'],
				'file'    => $log['file'],
				'type'    => $log['type'],
			);
		}

		return $clean_logs;
	}

		/**
		 * Get logs action.
		 *
		 * @since 2.5.0
		 * @return array<array<'datelog'|'message'|'user', string>>
		 */
	public static function get_logs_action() {
		/** @var array<array<'datelog'|'message'|'user', string>> $clear_logs */
		$clear_logs = self::get_clean_logs( 'logs_action' );

		$clean_logs = array();

		foreach ( $clear_logs as $log ) {
			$clean_logs[] = array(
				'datelog' => $log['datelog'],
				'message' => $log['message'],
				'user'    => $log['user'],
			);
		}

		return $clean_logs;
	}

	/**
	 * Clear old logs.
	 *
	 * @since 2.5.0
	 * @param array<array<'datelog'|'file'|'message'|'user'|'type', string>> $logs
	 * @return array<array<'datelog'|'file'|'message'|'user'|'type', string>>
	 */
	public static function clear_old_logs( $logs ) {
		$logs = array_filter(
			$logs,
			/**
			 * @param array<'datelog'|'file'|'message'|'type'|'user', string> $log
			 * @return bool
			 */
			function ( $log ) {
				$now      = new \DateTime();
				$log_date = new \DateTime( $log['datelog'] );

				$diff = $now->diff( $log_date );

				if ( $diff->days > 7 ) {
					return false;
				}

				return true;
			}
		);

		// a maximum of 100 lines must remain
		if ( count( $logs ) > 100 ) {
			array_splice( $logs, 0, count( $logs ) - 100 );
		}

		return $logs;
	}

	/**
	 * Add log to database.
	 *
	 * @since 2.5.0
	 * @param string $message
	 * @param string $file
	 * @param string $type
	 * @return void
	 */
	private static function add_log_to_db( $message, $file, $type ) {
		/** @var array<array<'datelog'|'file'|'message'|'type', string>> $current_log */
		$current_log = self::get_clean_logs( 'logs' );

		array_push(
			$current_log,
			array(
				'message' => $message,
				'file'    => $file,
				'type'    => $type,
				'datelog' => gmdate( 'Y-m-d H:i:s' ),
			)
		);

		Options::update( 'logs', $current_log );
	}

	/**
	 * Add log action to database.
	 *
	 * @since 2.6.9
	 * @param string $message
	 * @return void
	 */
	private static function add_log_action_to_db( $message ) {
		/** @var array<array<'datelog'|'message'|'user', string>> $current_log */
		$current_log = self::get_clean_logs( 'logs_action' );

		$current_user = wp_get_current_user();

		array_push(
			$current_log,
			array(
				'message' => $message,
				'user'    => $current_user->user_login . ' (ID: ' . $current_user->ID . ')',
				'datelog' => gmdate( 'Y-m-d H:i:s' ),
			)
		);

		Options::update( 'logs_action', $current_log );
	}

	/**
	 * Get clean logs.
	 *
	 * @since 2.6.9
	 * @param string $logs_name
	 * @return array<array<'datelog'|'file'|'message'|'user'|'type', string>>
	 */
	private static function get_clean_logs( $logs_name ) {
		$logs = Options::get( $logs_name, array() );

		if ( empty( $logs ) ) {
			return array();
		}

		if ( ! is_array( $logs ) ) {
			return array();
		}

		/** @var array<array<'datelog'|'file'|'message'|'user'|'type', string>> $logs */

		$current_log = self::clear_old_logs( $logs );

		return $current_log;
	}
}
