using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseImplement.Implements
{
    public class SupplierStorage : ISupplierStorage
    {
        public SupplierViewModel? Delete(SupplierBindingModel model)
        {
            using var context = new Database();
            var element = context.Suppliers
            .Include(x => x.Products)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Suppliers.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public SupplierViewModel? GetElement(SupplierSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Suppliers
            //.Include(x => x.Name)
            .Include(x => x.Products)
            .ThenInclude(x => x.Product)
            .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
        }

        public List<SupplierViewModel> GetFilteredList(SupplierSearchModel model)
        {
            if (string.IsNullOrEmpty(model.Name) && !model.Deals.HasValue)
            {
                return new();
            }
            using var context = new Database();
            if (!string.IsNullOrEmpty(model.Name))
            {
                return context.Suppliers
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => model.Name == x.Name)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
            return context.Suppliers
                .Include(x => x.Products)
                .ThenInclude(x => x.Product)
                .Where(x => model.Deals <= x.Deals)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<SupplierViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Suppliers
            .Include(x => x.Products)
            .ThenInclude(x => x.Product)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }

        public SupplierViewModel? Insert(SupplierBindingModel model)
        {
            using var context = new Database();
            var newProduct = Supplier.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Suppliers.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }

        public SupplierViewModel? Update(SupplierBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Suppliers.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(context, model);
                context.SaveChanges();
                product.UpdateProducts(context, model);
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}
