using Microsoft.EntityFrameworkCore;
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.SearchModels;
using SoftwareInstallationContracts.StoragesContracts;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoftwareInstallationDatabaseImplement.Implements
{
    public class PackageStorage : IPackageStorage
    {
        public List<PackageViewModel> GetFullList()
        {
            using var context = new SoftwareInstallationDatabase();
            return context.Packages
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        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)
                .Where(x => x.PackageName.Contains(model.PackageName))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }
        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 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(rec =>
                rec.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;
            }
        }
        public PackageViewModel? Delete(PackageBindingModel model)
        {
            using var context = new SoftwareInstallationDatabase();
            var element = context.Packages
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Packages.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
