namespace MyOffice.DbContext;

using Data.Models.Accounts;
using Data.Models.Currencies;
using Data.Models.Motions;
using Microsoft.EntityFrameworkCore;
using Data.Models.Users;

public enum AppDbContextProvidersEnum
{
	sqlite,
	mssql,
	npgsql
}

public class AppDbContext : DbContext
{
	private readonly AppDbContextProvidersEnum _provider;

	private readonly Dictionary<AppDbContextProvidersEnum, string> _noCaseCollation = new()
	{
		{ AppDbContextProvidersEnum.sqlite, "NOCASE" },
		{ AppDbContextProvidersEnum.npgsql, "my_ci_collation" }
	};

	public AppDbContext(AppDbContextProvidersEnum provider, DbContextOptions<AppDbContext> options) : base(options)
	{
		_provider = provider;
	}

	public DbSet<User> Users { get; set; } = null!;
	public DbSet<UserExternal> UserClaims { get; set; } = null!;

	public DbSet<CurrencyGlobal> CurrencyGlobals { get; set; } = null!;
	public DbSet<Currency> Currencies { get; set; } = null!;
	public DbSet<CurrencyRate> CurrencyRates { get; set; } = null!;
	public DbSet<AccountCategory> AccountCategories { get; set; } = null!;
	public DbSet<Account> Accounts { get; set; } = null!;
	public DbSet<AccountAccountCategory> AccountAccountCategories { get; set; } = null!;
	public DbSet<AccountAccess> AccountAccesses { get; set; } = null!;

	public DbSet<MotionGlobal> GlobalMotions { get; set; } = null!;
	public DbSet<MotionCategory> MotionCategories { get; set; } = null!;
	public DbSet<MotionAccount> Motions { get; set; } = null!;
	//public DbSet<AccountMotion> AccountMotions { get; set; } = null!;

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		var noCaseCollation = _noCaseCollation[_provider];

		if (_provider == AppDbContextProvidersEnum.npgsql)
			modelBuilder.HasCollation("my_ci_collation", "en-u-ks-primary", "icu", false);

		/* NOCASE PROPERTIES */
		modelBuilder.Entity<User>()
			.Property(x => x.UserName)
			.UseCollation(noCaseCollation);

		modelBuilder.Entity<User>()
			.Property(x => x.Email)
			.UseCollation(noCaseCollation);

		modelBuilder.Entity<UserExternal>()
			.Property(x => x.Provider)
			.UseCollation(noCaseCollation);

		modelBuilder.Entity<UserExternal>()
			.Property(x => x.Email)
			.UseCollation(noCaseCollation);


		modelBuilder.Entity<UserExternal>()
			.HasOne(x => x.User)
			.WithMany(x => x.UserClaims)
			.HasForeignKey(x => x.UserId);

		UserCreating(modelBuilder);
		CurrencyCreating(modelBuilder);
		AccountCreating(modelBuilder);
		MotionsCreating(modelBuilder);

		base.OnModelCreating(modelBuilder);
	}

	private void UserCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<User>()
			.HasOne(x => x.Currency)
			.WithMany()
			.HasForeignKey(x => x.CurrencyId);
	}

	private void CurrencyCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<CurrencyGlobal>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<Currency>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<CurrencyRate>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<Currency>()
			.HasOne(x => x.CurrencyGlobal)
			.WithMany(x => x.Currencies)
			.HasForeignKey(x => x.CurrencyGlobalId);

		modelBuilder.Entity<Currency>()
			.HasOne(x => x.User)
			.WithMany(x => x.Currencies)
			.HasForeignKey(x => x.UserId);

		modelBuilder.Entity<CurrencyRate>()
			.HasOne(x => x.Currency)
			.WithMany(x => x.Rates)
			.HasForeignKey(x => x.CurrencyId);
	}

	private void AccountCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<Account>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<AccountAccess>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<AccountMotion>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<AccountCategory>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<AccountAccountCategory>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<Account>()
			.HasOne(x => x.CurrencyGlobal)
			.WithMany(x => x.Accounts)
			.HasForeignKey(x => x.CurrencyGlobalId);

		modelBuilder.Entity<AccountAccess>()
			.HasOne(x => x.Account)
			.WithMany(x => x.AccessRights)
			.HasForeignKey(x => x.AccountId);

		modelBuilder.Entity<AccountAccess>()
			.HasOne(x => x.User)
			.WithMany(x => x.AccountAccess)
			.HasForeignKey(x => x.UserId);

		modelBuilder.Entity<AccountMotion>()
			.HasOne(x => x.Account)
			.WithMany(x => x.Motions)
			.HasForeignKey(x => x.AccountId);

		modelBuilder.Entity<AccountCategory>()
			.HasOne(x => x.User)
			.WithMany(x => x.AccountCategories)
			.HasForeignKey(x => x.UserId);

		modelBuilder.Entity<AccountAccountCategory>()
			.HasOne(x => x.Account)
			.WithMany(x => x.Categories)
			.HasForeignKey(x => x.AccountId);

		modelBuilder.Entity<AccountAccountCategory>()
			.HasOne(x => x.Category)
			.WithMany(x => x.Accounts)
			.HasForeignKey(x => x.CategoryId);
	}

	private void MotionsCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<MotionGlobal>()
			.HasKey(x => x.Id);
		modelBuilder.Entity<MotionCategory>()
			.HasKey(x => x.Id);
		modelBuilder.Entity<MotionAccount>()
			.HasKey(x => x.Id);
		modelBuilder.Entity<AccountMotion>()
			.HasKey(x => x.Id);

		modelBuilder.Entity<MotionCategory>()
			.HasOne(x => x.User)
			.WithMany(x => x.MotionCategories)
			.HasForeignKey(x => x.UserId);

		modelBuilder.Entity<MotionAccount>()
			.HasOne(x => x.Category)
			.WithMany(x => x.Motions)
			.HasForeignKey(x => x.CategoryId);

		modelBuilder.Entity<MotionAccount>()
			.HasOne(x => x.MotionGlobal)
			.WithMany(x => x.Motions)
			.HasForeignKey(x => x.MotionGlobalId);

		/*modelBuilder.Entity<AccountMotion>()
			.HasOne(x => x.Motion)
			.WithMany(x => x.Motions)
			.HasForeignKey(x => x.MotionId);

		modelBuilder.Entity<AccountMotion>()
			.HasOne(x => x.Account)
			.WithMany(x => x.Motions)
			.HasForeignKey(x => x.AccountId);*/
	}
}