using System.Reflection;
using DbUp;
using Microsoft.Extensions.Configuration;

namespace EnotaryoPH.DbMigration
{
    internal class Program
    {
        private static int Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;

            var config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();
            var connectionString =
                args.FirstOrDefault()
                ?? config.GetConnectionString("DbUpConnection");

            Console.WriteLine($"Connecting to {connectionString}");

            var upgrader =
                DeployChanges.To
                    .PostgresqlDatabase(connectionString)
                    .WithVariablesDisabled()
                    .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                    .LogToConsole()
                    .Build();

            if (upgrader.TryConnect(out var err))
            {
                var result = upgrader.PerformUpgrade();

                if (!result.Successful)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(result.Error);
                    Console.ResetColor();
#if DEBUG
                    Console.ReadLine();
#endif
                    return -1;
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Cannot Connect: '{err}'");
                Console.ResetColor();
                return -1;
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Success!");
            Console.ResetColor();
            return 0;
        }
    }
}