using Sqler.Config;

using Vit.Core.Module.Serialization;

using Vitorm;


namespace Sqler.Module.Sqler.Logical.DataEditor
{
    public class Settings
    {
        static Settings()
        {
            Data.TryCreateTable<SettingEntity>();
        }
        public static void Set(string key, string value)
        {
            var entity = Data.Get<SettingEntity>(key);

            if (entity != null)
            {
                entity.value = value;
                Data.Update(entity);
            }
            else
            {
                entity = new() { name = key, value = value };
                Data.Add(entity);
            }

        }
        public static void Set<T>(string key, T value)
        {
            Set(key, Json.Serialize(key));
        }

        public static string Get(string key)
        {
            return Data.Get<SettingEntity>(key)?.value;
        }

        public static T Get<T>(string key)
        {
            return Json.Deserialize<T>(Get(key));
        }


    }
}



namespace Sqler.Config
{
    [System.ComponentModel.DataAnnotations.Schema.Table("Sqler_Setting")]
    public class SettingEntity
    {
        /// <summary>
        /// {schema_name}.{table_name}
        /// </summary>
        [System.ComponentModel.DataAnnotations.Key]
        public string name { get; set; }

        public string value { get; set; }

        public string remarks { get; set; }
        public string ext { get; set; }
    }
}