using e_suite.Database.Migrator.Migrations;

namespace e_suite.Database.Migrator.SqlWrapper;

public static class PrePostScriptRunner
{
    public static void RunScripts(string connectionString, string databaseName, ScriptType scriptType)
    {
        Console.WriteLine($"Running {scriptType} scripts");
        try
        {
            var parsedScript = ScriptParser.GetScriptText(scriptType, databaseName);
            if (parsedScript != string.Empty)
            {
                try
                {
                    using var sqlWrapper = new SqlWrapper(connectionString);
                    sqlWrapper.CommandText = parsedScript;
                    sqlWrapper.Execute();
                    Console.WriteLine($"{scriptType} scripts completed");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{scriptType} scripts skipped - Unable to connect to sql server");
                    Console.WriteLine(e);
                }
            }
            else
            {
                Console.WriteLine($"{scriptType} scripts skipped - no content");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine($"{scriptType} scripts failed");
            Console.WriteLine(e);
            throw;
        }
    }
}