using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Hcs.WebApp.Data.Hcs
{
    public class HcsDbContext(DbContextOptions<HcsDbContext> options) : DbContext(options)
    {
        public DbSet<Registry> Registries { get; set; }

        public DbSet<RegistryElement> Elements { get; set; }

        public DbSet<Campaign> Campaigns { get; set; }

        public DbSet<Operation> Operations { get; set; }

        public DbSet<House> Houses { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseSeeding((context, _) =>
                {
                    if (context.Set<Registry>().Count() == 0)
                    {
                        var registries = GetDefaultRegistries();
                        context.Set<Registry>().AddRange(registries);
                        context.SaveChanges();
                    }
                })
                .UseAsyncSeeding(async (context, _, cancellationToken) =>
                {
                    if (!await context.Set<Registry>().AnyAsync())
                    {
                        var registries = GetDefaultRegistries();
                        await context.Set<Registry>().AddRangeAsync(registries);
                        await context.SaveChangesAsync();
                    }
                });
        }

        private IEnumerable<Registry> GetDefaultRegistries()
        {
            var result = new List<Registry>
            {
                new()
                {
                    Number = 2,
                    Name = "Коммунальный ресурс",
                    IsCommon = true
                },
                new()
                {
                    Number = 3,
                    Name = "Вид коммунальной услуги",
                    IsCommon = true
                },
                new()
                {
                    Number = 16,
                    Name = "Межповерочный интервал",
                    IsCommon = true
                },
                new()
                {
                    Number = 24,
                    Name = "Состояние дома",
                    IsCommon = true
                },
                new()
                {
                    Number = 27,
                    Name = "Тип прибора учета",
                    IsCommon = true
                },
                new()
                {
                    Number = 32,
                    Name = "Часовые зоны по Olson",
                    IsCommon = true
                },
                new()
                {
                    Number = 51,
                    Name = "Вид коммунальной услуги",
                    IsCommon = false
                },
                new()
                {
                    Number = 58,
                    Name = "Основание заключения договора",
                    IsCommon = true
                },

                new()
                {
                    Number = 239,
                    Name = "Тарифицируемый ресурс",
                    IsCommon = true
                },
                new()
                {
                    Number = 276,
                    Name = "Показатели качества коммунальных ресурсов",
                    IsCommon = true
                },
                new()
                {
                    Number = 338,
                    Name = "Стадия жизненного цикла",
                    IsCommon = true
                }
            };
            return result;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Campaign>()
                .Property(x => x.Type)
                .HasConversion(new EnumToStringConverter<Campaign.CampaignType>());

            modelBuilder.Entity<Operation>()
                .Property(x => x.Type)
                .HasConversion(new EnumToStringConverter<Operation.OperationType>());
        }
    }
}
