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

namespace SchoolDatabase.Models
{
    public class ItemForStudy : IItemForStudyModel
	{
		public int Id { get; private set; }
		[Required]
		public int ItemId { get; private set; }
		[Required]
		public int StudyId { get; private set; }
		public virtual Item Item { get; set; } = new();
		public virtual Study Study { get; set; } = new();

		public static ItemForStudy? Create(SchoolDbContext context, ItemForStudyBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}

			return new ItemForStudy
			{
				Item = context.Items.First(x => x.Id == model.ItemId),
				Study = context.Studys.First(x => x.Id == model.StudyId)
			};
		}

		public void Update(SchoolDbContext context, ItemForStudyBindingModel? model)
		{
			if (model == null)
			{
				return;
			}

			Item = context.Items.First(x => x.Id == model.ItemId);
			Study = context.Studys.First(x => x.Id == model.StudyId);
		}

		public ItemForStudyViewModel GetViewModel => new ItemForStudyViewModel
		{
			Id = Id,
			ItemId = ItemId,
			StudyId = StudyId,
			ItemName = Item.Name
		};
	}
}
