using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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 StudentStorage : IStudentStorage
    {
        public StudentViewModel? GetElement(StudentSearchModel model)
        {
            if (!model.Id.HasValue && model.StudentCard <= 0)
            {
                return null;
            }
            using var context = new Database();
            return context.Students
                .Include(record => record.User)
                .Include(record => record.EducationStatus)
                .FirstOrDefault(record => record.Id.Equals(model.Id) 
                    || record.StudentCard.Equals(model.StudentCard))
                ?.GetViewModel;
        }
        public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
        {
            using var context = new Database();
            if (model.Id.HasValue)
            {
                return context.Students
                .Include(record => record.User)
                .Include(record => record.EducationStatus)
                .Where(record => record.Id.Equals(model.Id))
                .Select(record => record.GetViewModel)
                .ToList();
            }
            else if (model.UserId.HasValue)
            {
                return context.Students
                   .Include(record => record.User)
                   .Include(record => record.EducationStatus)
                   .Where(record => record.UserId == model.UserId)
                   .Select(record => record.GetViewModel)
                   .ToList();
            }
            else
            {
                return new();
            }
        }
        public List<StudentViewModel> GetFullList()
        {
            using var context = new Database();
            return context.Students
                .Include(record => record.User)
                .Include(record => record.EducationStatus)
                .Select(record => record.GetViewModel)
                .ToList();
        }
        public StudentViewModel? Insert(StudentBindingModel model)
        {
            var newStudent = Student.Create(model);
            if (newStudent == null)
            {
                return null;
            }
            using var context = new Database();
            context.Students.Add(newStudent);
            context.SaveChanges();
            return newStudent.GetViewModel;
        }
        public StudentViewModel? Update(StudentBindingModel model)
        {
            using var context = new Database();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var student = context.Students
                    .Include(record => record.User)
                    .Include(record => record.EducationStatus)
                    .FirstOrDefault(record => record.Id.Equals(model.Id));
                if (student == null)
                {
                    return null;
                }
                student.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return student.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public StudentViewModel? Delete(StudentBindingModel model)
        {
            using var context = new Database();
            var student = context.Students
                .Include(record => record.User)
                .Include(record => record.EducationStatus)
                .FirstOrDefault(record => record.Id.Equals(model.Id));
            if (student == null)
            {
                return null;
            }
            context.Students.Remove(student);
            context.SaveChanges();
            return student.GetViewModel;
        }
    }
}
