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 Courses: ICoursesModel
	{
		public int Id { get; private set; }
		[Required]
		public string Name { get; private set; } = string.Empty;
		[Required, Column(TypeName = "decimal (10,2)")]
		public decimal Price { get; private set; }
		[Required, Column(TypeName = "decimal (3,1)")]
		public decimal Duration { get; private set; }
		[Required]
		public int WorkerId { get; private set; }
		
		// Курсы в обучении
		[ForeignKey("CoursesId")]
		public virtual List<CoursesForStudy> CoursessForStudy { get; set; } = new();
		public virtual Worker Worker { get; set; } = new();
		public static Courses? Create(SchoolDbContext context, CoursesBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}
			return new()
			{
				Name = model.Name,
				Price = model.Price,
				Duration = model.Duration,
				Worker = context.Workers.First(x => x.Id == model.WorkerId)
			};
		}
		public void Update(SchoolDbContext context, CoursesBindingModel? model)
		{
			if (model == null)
			{
				return;
			}
			Name = model.Name;
			Price = model.Price;
			Duration = model.Duration;
			Worker = context.Workers.First(x => x.Id == model.WorkerId);
		}
		public CoursesViewModel GetViewModel => new()
		{
			Id = Id,
			Name = Name,
			Price = Price,
			Duration = Duration,
			WorkerId = WorkerId,
		};
	}
}
