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

namespace SchoolDatabase.Models
{
	public class Study : IStudyModel
	{
		public int Id { get; private set; }
		[Required, Column(TypeName = "date")]
		public DateTime DateCreated { get; private set; } = DateTime.Now;
		[Required, Column(TypeName = "date")]
		public DateTime DateСompletion { get; private set; } = DateTime.Now;
		[Required]
		public int CustumerId { get; private set; }

		// Курсы в обучении
		[ForeignKey("StudyId")]
		public virtual List<ICoursesForStudy> ICoursessForStudy { get; set; } = new();
		
		// Затраты на обучение
		[ForeignKey("StudyId")]
		public virtual List<ItemForStudy> ItemsForStudy { get; set; } = new();
		public virtual Custumer Custumer { get; set; } = new();
		public static Study? Create(SchoolDbContext context, StudyBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}
			return new()
			{
				DateCreated = model.DateCreated,
				DateСompletion = model.DateСompletion,
				Custumer = context.Custumers.First(x => x.Id == model.CustumerId)
			};
		}
		public void Update(SchoolDbContext context, StudyBindingModel? model)
		{
			if (model == null)
			{
				return;
			}
			DateCreated = model.DateCreated;
				DateСompletion = model.DateСompletion;
			Custumer = context.Custumers.First(x => x.Id == model.CustumerId);
		}
		public StudyViewModel GetViewModel => new()
		{
			Id = Id,
			DateCreated = DateCreated,
			DateСompletion = DateСompletion,
			CustumerId = CustumerId,
			CustumerFIO = Custumer.FIO
		};
	}
}
