using Elwig.Helpers;
using System.Reflection;

namespace Tests.E2ETests {
    [SetUpFixture]
    public static class Setup {

        private static WinAppDriver? Driver;

        [OneTimeSetUp]
        public static void SetupWinAppDriver() {
            Driver = new();
        }

        [OneTimeSetUp]
        public static async Task SetupDatabase() {
            if (File.Exists(Utils.TestDatabasePath)) File.Delete(Utils.TestDatabasePath);
            using var cnx = await AppDbContext.ConnectAsync($"Data Source=\"{Utils.TestDatabasePath}\"; Mode=ReadWriteCreate; Foreign Keys=True; Cache=Default; Pooling=False");
            await cnx.ExecuteEmbeddedScript(Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.Create.sql");
            await cnx.ExecuteEmbeddedScript(Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.Insert.sql");
            await cnx.ExecuteEmbeddedScript(Assembly.GetExecutingAssembly(), "Tests.Resources.Sql.E2EInsert.sql");
        }

        [OneTimeTearDown]
        public static void TeardownWinAppDriver() {
            Driver?.Dispose();
        }

        [OneTimeTearDown]
        public static void TeardownDatabase() {
            try {
                // FIXME not working - other process using file
                File.Delete(Utils.TestDatabasePath);
            } catch { }
        }
    }
}
