using FWLAZData.Data;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FWLAZData
{
    public class LocalDbContext : DbContext
    {
        static string? ConnectionString;
        internal static string DatabaseFile { get; private set; } = "quiz.db";


        public DbSet<Quiz> Quiz { get; set; }
        public DbSet<QuestionGroup> QuestionGroup { get; set; }
        public DbSet<Question> Question { get; set; }
        public DbSet<Answer> Answers { get; set; }

        public LocalDbContext() { }
        public LocalDbContext(DbContextOptions options) : base(options) { }

        public static void SetConnectionString(string connectionstring)
        {
            var connstring = new SqliteConnectionStringBuilder(connectionstring);
            DatabaseFile = connstring.DataSource; // Benötigt um das Unterverzeichnis zu ermitteln und ggf. zu erstellen falls nicht vorhanden
            ConnectionString = connstring.ToString();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {            
            if (!optionsBuilder.IsConfigured)
            {
                // Aktuell noch keine andere Möglichkeit gefunden, damit trotzdem dotnet ef migrations add, dotnet ef database update etc. ausgeführt werden kann.
                if (ConnectionString == null) SetConnectionString($"Data Source = {DatabaseFile}");
                optionsBuilder.UseSqlite(ConnectionString);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            
        }
    }
}
