using System.Reflection;
using BO.Console.Commands.Base;
using Quadarax.Foundation.Core.Data.Extensions;
using Quadarax.Foundation.Core.QConsole;
using Quadarax.Foundation.Core.QConsole.Attributes;
using Quadarax.Foundation.Core.Resource.Extensions;
using Quadarax.Foundation.Core.Value;

namespace BO.Console.Commands.Install
{
    [CommandDefinition]
    internal class ClearDbCommand : DbBaseCommand
    {
        #region *** Constants ***
        private const string CS_CMD_CLEAR_NAME = "dbclear";
        private const string CS_CMD_CLEAR_DESCR = "clear BO database (keep structure and deletes data, reseed identifiers).";

        private const string DB_PROP_NAME = "BO_Version";

        #endregion

        #region *** Properties ***

        public override string Name => CS_CMD_CLEAR_NAME;
        public override string Description => CS_CMD_CLEAR_DESCR;

        #endregion

        #region *** Constructor ***
        public ClearDbCommand(Engine engine) : base(engine)
        {
        }
        #endregion

        #region *** EXECUTE ***
        protected override Result OnExecute()
        {
            var result = new Result();
            using (var conn = OpenConnection())
            {
                var script = Assembly.GetExecutingAssembly()
                    .GetEmbeddedFileContentAsString("BO.Console.Scripts.bo_clear_data.sql");
                WriteCaption($"Clearing all data in '{DatabseName}'");
                var resultExecute = conn.BatchExecute(script);
                WriteDebugInfo($"Deploy script returns: {resultExecute}");
                var version = conn.DatabasePropertyGet(DB_PROP_NAME);
                WriteCaption($"Database '{DatabseName}' cleared in version '{version}'");
            }

            return new Result();
        }
        #endregion

        #region *** Private operations ***


        #endregion
    }
}
