using Microsoft.EntityFrameworkCore;
using RenovationWorkContracts.BindingModels;
using RenovationWorkContracts.SeatchModels;
using RenovationWorkContracts.StorageContracts;
using RenovationWorkContracts.ViewModels;
using RenovationWorkDatabaseImplement.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RenovationWorkDatabaseImplement.Implements
{
    public class RepairStorage : IRepairStorage
    {
        public List<RepairViewModel> GetFullList()
        {
            using var context = new RenovationWorkDatabase();
            return context.Repairs
                    .Include(x => x.Components)
                    .ThenInclude(x => x.Component)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<RepairViewModel> GetFilteredList(RepairSearchModel model)
        {
            if (string.IsNullOrEmpty(model.RepairName))
            {
                return new();
            }
            using var context = new RenovationWorkDatabase();
            return context.Repairs
                    .Include(x => x.Components)
                    .ThenInclude(x => x.Component)
                    .Where(x => x.RepairName.Contains(model.RepairName))
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public RepairViewModel? GetElement(RepairSearchModel model)
        {
            if (string.IsNullOrEmpty(model.RepairName) && !model.Id.HasValue)
            {
                return null;
            }
            using var context = new RenovationWorkDatabase();
            return context.Repairs
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RepairName) && x.RepairName == model.RepairName) ||
                                (model.Id.HasValue && x.Id == model.Id))
                ?.GetViewModel;
        }

        public RepairViewModel? Insert(RepairBindingModel model)
        {
            using var context = new RenovationWorkDatabase();
            var newRepair = Repair.Create(context, model);
            if (newRepair == null)
            {
                return null;
            }
            context.Repairs.Add(newRepair);
            context.SaveChanges();
            return newRepair.GetViewModel;
        }

        public RepairViewModel? Update(RepairBindingModel model)
        {
            using var context = new RenovationWorkDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var Repair = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
                if (Repair == null)
                {
                    return null;
                }
                Repair.Update(model);
                context.SaveChanges();
                Repair.UpdateComponents(context, model);
                transaction.Commit();
                return Repair.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public RepairViewModel? Delete(RepairBindingModel model)
        {
            using var context = new RenovationWorkDatabase();
            var element = context.Repairs
                .Include(x => x.Components)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Repairs.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
