<?php
/**
 * Logger — stores up to 50 import/export operations in wp_options.
 *
 * Each entry:
 * {
 *   "date"      : "2026-06-25 14:32:00",
 *   "type"      : "import" | "export",
 *   "mode"      : "standard",
 *   "post_type" : "supplier",
 *   "file"      : "suppliers.xlsx",   // import only
 *   "format"    : "xlsx",             // export only
 *   "status"    : "success" | "warning" | "error",
 *   "summary"   : { "created": 3, "updated": 12, "skipped": 1, "errors": 0 }
 * }
 *
 * @package Ideo_Field_Porter
 */

namespace IdeoFieldPorter;

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

class Logger {

	const OPTION_KEY = 'ideo_fp_logs';
	const MAX_ENTRIES = 500;

	/**
	 * Append one log entry (FIFO, max 50).
	 *
	 * @param array $entry Associative array with keys: type, mode, post_type,
	 *                     file|format, status, summary.
	 */
	public static function log( array $entry ) {
		$logs = get_option( self::OPTION_KEY, array() );
		if ( ! is_array( $logs ) ) $logs = array();

		$entry['date'] = current_time( 'Y-m-d H:i:s' );

		array_unshift( $logs, $entry );

		if ( count( $logs ) > self::MAX_ENTRIES ) {
			$logs = array_slice( $logs, 0, self::MAX_ENTRIES );
		}

		update_option( self::OPTION_KEY, $logs, false );
	}

	/**
	 * Return all log entries.
	 *
	 * @return array
	 */
	public static function get_all() {
		$logs = get_option( self::OPTION_KEY, array() );
		return is_array( $logs ) ? $logs : array();
	}

	/**
	 * Delete all log entries.
	 */
	public static function clear() {
		delete_option( self::OPTION_KEY );
	}
}
