using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;

namespace UniversityDataBaseImplemet.Implements
{
    public class DocumentStorage : IDocumentStorage
    {
        public DocumentViewModel? GetElement(DocumentSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Documents
                .Include(record => record.User)
                .Include(record => record.Students)
                    .ThenInclude(record => record.Student)
                .FirstOrDefault(record => record.Id == model.Id
                    || record.Name.Equals(model.Name))
                ?.GetViewModel;
        }
        public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
        {
            using var context = new Database();
            if (model.Id.HasValue)
            {
                return context.Documents
                    .Include(record => record.User)
                    .Include(record => record.Students)
                    .ThenInclude(record => record.Student)
                    .Where(record => record.Id.Equals(model.Id))
                    .Select(record => record.GetViewModel)
                    .ToList();
            }
            else if (model.DateFrom != null && model.DateTo != null && model.UserId.HasValue)
            {
                return context.Documents
                    .Where(d => d.Date >= model.DateFrom && d.Date <= model.DateTo && d.UserId == model.UserId)
                    .Include(d => d.Students)
                        .ThenInclude(sd => sd.Student)
                            .ThenInclude(s => s.StudentStream)
                                .ThenInclude(ss => ss.Stream)
                    .Include(d => d.Students)
                        .ThenInclude(sd => sd.Student)
                            .ThenInclude(s => s.EducationStatus)                    
                    .Select(result => result.GetViewModel)
                    .ToList();
            }
            else if (model.UserId.HasValue)
            {
                return context.Documents
                   .Include(record => record.User)
                   .Include(record => record.Students)
                        .ThenInclude(record => record.Student)
                   .Where(record => record.UserId == model.UserId)
                   .Select(record => record.GetViewModel)
                   .ToList();
            }            
            else
            {
                return new();
            }
        }
        public List<DocumentViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Documents
                .Include(record => record.User)
                .Include(record => record.Students)
                    .ThenInclude(record => record.Student)
                .Select(record => record.GetViewModel)
                .ToList();
        }
        public DocumentViewModel? Insert(DocumentBindingModel model)
        {
            var newDocument = Document.Create(model);
            if(newDocument == null) 
            {
                return null;
            }
            using var context = new Database();
            context.Documents.Add(newDocument);
            context.SaveChanges();
            return newDocument.GetViewModel;
        }
        public DocumentViewModel? Update(DocumentBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var document = context.Documents
                    .Include(record => record.User)
                    .FirstOrDefault(record => record.Id.Equals(model.Id));
                if (document == null)
                {
                    return null;
                }
                document.Update(model);
                context.SaveChanges();
                document.UpdateStudents(context, model);
                transaction.Commit();
                return document.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public DocumentViewModel? Delete(DocumentBindingModel model)
        {
            using var context = new Database();
            var document = context.Documents
                .Include(record => record.User)
                .FirstOrDefault(record => record.Id.Equals(model.Id));
            if (document == null)
            {
                return null;
            }
            context.Documents.Remove(document);
            context.SaveChanges();
            return document.GetViewModel;
        }
    }
}
