using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using Stream = UniversityDataBaseImplemet.Models.Stream;

namespace UniversityDataBaseImplemet.Implements
{
    public class StreamStorage:IStreamStorage
    {
        public StreamViewModel? GetElement(StreamSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new Database();
            return context.Streams
				.Include(record => record.StreamStudents)
					.ThenInclude(record => record.Student)
				.FirstOrDefault(record => record.Id == model.Id
                    || record.Name.Equals(model.Name))
                ?.GetViewModel;
        }
        public List<StreamViewModel> GetFilteredList(StreamSearchModel model)
        {
            using var context = new Database();
            if (model.Id.HasValue)
            {
                return context.Streams
					.Include(record => record.StreamStudents)
					.ThenInclude(record => record.Student)
					.Where(record => record.Id.Equals(model.Id))
                    .Select(record => record.GetViewModel)
                    .ToList();
            }
            else if (model.UserId.HasValue)
            {
                return context.Streams
				   .Include(record => record.StreamStudents)
				   .ThenInclude(record => record.Student)
				   .Where(record => record.UserId == model.UserId)
                   .Select(record => record.GetViewModel)
                   .ToList();
            }
            else
            {
                return new();
            }
        }
        public List<StreamViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Streams
				.Include(record => record.StreamStudents)
					.ThenInclude(record => record.Student)
				.Select(record => record.GetViewModel)
                .ToList();
        }
        public StreamViewModel? Insert(StreamBindingModel model)
        {
            var newStream = Stream.Create(model);
            if (newStream == null)
            {
                return null;
            }
            using var context = new Database();
            context.Streams.Add(newStream);
            context.SaveChanges();
            return newStream.GetViewModel;
        }
        public StreamViewModel? Update(StreamBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var stream = context.Streams
                    .FirstOrDefault(record => record.Id.Equals(model.Id));
                if (stream == null)
                {
                    return null;
                }
                stream.Update(model);
                context.SaveChanges();
				stream.UpdateStreamStudents(context, model);
				transaction.Commit();
                return stream.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public StreamViewModel? Delete(StreamBindingModel model)
        {
            using var context = new Database();
            var stream = context.Streams
                .FirstOrDefault(record => record.Id.Equals(model.Id));
            if (stream == null)
            {
                return null;
            }
            context.Streams.Remove(stream);
            context.SaveChanges();
            return stream.GetViewModel;
        }
    }
}
