using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDatabaseImplement;
using SoftwareInstallationDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;

namespace SoftwareInstallationDatabaseImplement.Implements
{
    public class PackageStorage : IPackageStorage
    {
        public PackageViewModel? Delete(PackageBindingModel model)
        {
            using var context = new SoftwareInstallationDatabase();
            var element = context.Packages.FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                context.Packages.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public PackageViewModel? GetElement(PackageSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new SoftwareInstallationDatabase();
            return context.Packages
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .FirstOrDefault
                (x => (!string.IsNullOrEmpty(model.PackageName) && x.PackageName == model.PackageName) ||
                      (model.Id.HasValue && x.Id == model.Id)
                )?.GetViewModel;
        }

        public List<PackageViewModel> GetFilteredList(PackageSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PackageName))
            {
                return new();
            }
            using var context = new SoftwareInstallationDatabase();
            return context.Packages
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .Select(x => x.GetViewModel)
                .Where(x => x.PackageName.Contains(model.PackageName))
                .ToList();
        }

        public List<PackageViewModel> GetFullList()
        {
            using var context = new SoftwareInstallationDatabase();
            return context.Packages
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public PackageViewModel? Insert(PackageBindingModel model)
        {
            using var context = new SoftwareInstallationDatabase();
            var newPackage = Package.Create(context, model);
            if (newPackage == null)
            {
                return null;
            }
            context.Packages.Add(newPackage);
            context.SaveChanges();
            return newPackage.GetViewModel;
        }

        public PackageViewModel? Update(PackageBindingModel model)
        {
            using var context = new SoftwareInstallationDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var package = context.Packages.FirstOrDefault(x => x.Id == model.Id);
                if (package == null)
                {
                    return null;
                }
                package.Update(model);
                context.SaveChanges();
                package.UpdateComponents(context, model);
                transaction.Commit();
                return package.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
