using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityModels.Models;

namespace UniversityDataBaseImplemet.Models
{
    public class Document : IDocumentModel
    {
        public int Id { get; private set; }
        [Required]
        public string Name { get; set; } = string.Empty;
        [Required]
        public DateTime Date { get; set; }
        [Required]
        public int UserId { get; set; }
        [ForeignKey("DocumentId")]
        public virtual List<StudentDocument> Students { get; set; } = new();
        [ForeignKey("DocumentId")]
        public virtual List<EducationGroupDocument> EducationGroupDocument { get; set; } = new();
        public virtual User User { get; set; }
        private Dictionary<int, IStudentModel>? _studentDocument = null;
        [NotMapped]
        public Dictionary<int, IStudentModel> StudentDocument
        {
            get
            {
                if (_studentDocument == null)
                {
                    _studentDocument = Students.ToDictionary(rec => rec.StudentId, rec => rec.Student as IStudentModel);
                }
                return _studentDocument;
            }
        }
        public static Document? Create(DocumentBindingModel model)
        {
            return new Document()
            {
                Id = model.Id,
                Name = model.Name,
                Date = model.Date,
                UserId = model.UserId,
            };
        }
        public void Update(DocumentBindingModel model)
        {
            if(model == null)
            {
                return;
            }
            Name = model.Name;
            Date = model.Date;
            UserId = model.UserId;
        }
        public void UpdateStudents(Database context, DocumentBindingModel model)
        {
            var studentDocument = context.StudentDocuments
                .Where(rec => rec.DocumentId == model.Id)
                .ToList();
            if (studentDocument != null)
            {
                context.StudentDocuments
                    .RemoveRange(studentDocument
                            .Where(rec => !model.StudentDocument
                            .ContainsKey(rec.StudentId))
                        );
                context.SaveChanges();
                var document = context.Documents
                    .First(x => x.Id == Id);
                foreach (var sd in studentDocument)
                {
                    model.StudentDocument.Remove(sd.StudentId);
                }
                foreach (var sd in model.StudentDocument)
                {
                    context.StudentDocuments.Add(new StudentDocument
                    {
                        Document = document,
                        Student = context.Students.First(x => x.Id == sd.Key),
                    });
                    context.SaveChanges();
                }
                _studentDocument = null;
            }
        }
        public DocumentViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Date = Date,
            UserId = UserId,     
            StudentDocument = StudentDocument
        };
    }
}
