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

namespace SchoolDatabase.Models
{
	public class Payment : IPaymentModel
	{
		public int Id { get; private set; }
		[Required, Column(TypeName = "date")]
		public DateTime DatePayment { get; private set; } = DateTime.Now;
		[Required, Column(TypeName = "decimal (10,2)")]
		public decimal Sum { get; private set; }
		[Required]
		public int CoursesForStudyId { get; private set; }
		public virtual CoursesForStudy CoursesForStudy { get; set; } = new();
		public static Payment? Create(SchoolDbContext context, PaymentBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}
			return new()
			{
				DatePayment = model.DatePayment,
				Sum = model.Sum,
				CoursesForStudy = context.CoursessForStudys.First(x => x.Id == model.CoursesForStudyId)
			};
		}
		public void Update(SchoolDbContext context, PaymentBindingModel? model)
		{
			if (model == null)
			{
				return;
			}
			DatePayment = model.DatePayment;
			Sum = model.Sum;
			CoursesForStudy = context.CoursessForStudys.First(x => x.Id == model.CoursesForStudyId);
		}
		public PaymentViewModel GetViewModel => new()
		{
			Id = Id,
			DatePayment = DatePayment,
			Sum = Sum,
			CoursesForStudyId = CoursesForStudyId
		};
	}
}
