using Contracts.BindingModels;
using Contracts.SearchModel;
using Contracts.Storage;
using Contracts.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBase.Implements
{
    public class CompanyStorage : ICompanyStorage
    {
        public List<CompanyVM> GetFullList()
        {
            using var context = new LogisticContext();
            return context.Companies.FromSqlRaw("Select * From Company")
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
        }

        public List<CompanyVM> GetFilteredList(CompanySM model)
        {
            if (!model.StatusId.HasValue )
            {
                return new();
            }
            using var context = new LogisticContext();
                return context.Companies
                        .FromSqlRaw("Select * FROM Company WHERE StatusId = {0}", model.StatusId)
                        .ToList()
                        .Select(x => x.GetViewModel)
                        .ToList();
            
        }

        public CompanyVM? GetElement(CompanySM model)
        {
            if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title))
            {
                return null;
            }
            using var context = new LogisticContext();
            return context.Companies
                .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Title) && x.Title.Equals(model.Title)))
                ?.GetViewModel;
        }

        public CompanyVM? Insert(CompanyBM model)
        {
            using var context = new LogisticContext();
            var newCompany = Company.Create(context, model);
            if (newCompany == null)
            {
                return null;
            }
            context.Companies.Add(newCompany);
            context.SaveChanges();
            return newCompany.GetViewModel;
        }

        public CompanyVM? Update(CompanyBM model)
        {
            using var context = new LogisticContext();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var company = context.Companies.FirstOrDefault(rec => rec.Id == model.Id);
                if (company == null)
                {
                    return null;
                }
                company.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return company.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

        public CompanyVM? Delete(CompanyBM model)
        {
            using var context = new LogisticContext();
            var element = context.Companies
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Companies.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}