using UnityEngine;

namespace AIBridge.Editor
{
    /// <summary>
    /// Logger utility for AI Bridge with consistent prefix
    /// </summary>
    public static class AIBridgeLogger
    {
        private const string PREFIX = "[AIBridge]";
        private static EditorOption<bool> _debugEnabledOption;

        /// <summary>
        /// Enable or disable debug logging
        /// </summary>
        public static bool DebugEnabled
        {
            get
            {
                if (_debugEnabledOption == null)
                {
                    _debugEnabledOption = new EditorOption<bool>("AIBridge_DebugLogging", false, ReadDebugEnabled, WriteDebugEnabled);
                }
                return _debugEnabledOption.Value;
            }
            set
            {
                if (_debugEnabledOption == null)
                {
                    _debugEnabledOption = new EditorOption<bool>("AIBridge_DebugLogging", false, ReadDebugEnabled, WriteDebugEnabled);
                }
                _debugEnabledOption.Value = value;
            }
        }

        public static void LogDebug(string message)
        {
            if (DebugEnabled)
            {
                Debug.Log($"{PREFIX} {message}");
            }
        }

        public static void LogInfo(string message)
        {
            Debug.Log($"{PREFIX} {message}");
        }

        public static void LogWarning(string message)
        {
            Debug.LogWarning($"{PREFIX} {message}");
        }

        public static void LogError(string message)
        {
            Debug.LogError($"{PREFIX} {message}");
        }

        private static bool ReadDebugEnabled(string key, bool defaultValue)
        {
            return AIBridgeProjectSettings.Instance.DebugLogging;
        }

        private static void WriteDebugEnabled(string key, bool value)
        {
            var settings = AIBridgeProjectSettings.Instance;
            if (settings.DebugLogging == value)
            {
                return;
            }

            settings.DebugLogging = value;
            settings.SaveSettings();
        }
    }
}
