using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DatabaseImplement.Models
{
	public class Detail : IDetailModel
	{
		public int Id { get; set; }
		[Required]
		public string Name { get; set; } = string.Empty;
		[Required]
		public double Cost { get; set; }
		[Required]
		public DateTime DateCreate { get; set; } = DateTime.Now;
		[Required]
		public int UserId { get; set; }
		[ForeignKey("DetailId")]
		public virtual List<DetailProduct> DetailProducts { get; set; } = new();
		[ForeignKey("DetailId")]
		public virtual List<DetailProduction> DetailProductions { get; set; } = new();
		public virtual Implementer User { get; set; }
		public static Detail? Create(DetailBindingModel model)
		{
			if (model == null)
			{
				return null;
			}
			return new Detail
			{
				Id = model.Id,
				Name = model.Name,
				Cost = model.Cost,
				UserId = model.UserId,
				DateCreate = model.DateCreate
			};
		}
		public static Detail Create(DetailViewModel model)
		{
			return new Detail
			{
				Id = model.Id,
				Name = model.Name,
				Cost = model.Cost,
				UserId = model.UserId,
				DateCreate = model.DateCreate
			};
		}
		public void Update(DetailBindingModel model)
		{
			if (model == null)
				return;
			Name = model.Name;
			Cost = model.Cost;
		}
		public DetailViewModel GetViewModel => new()
		{
			Id = Id,
			Name = Name,
			Cost = Cost,
			UserId = UserId,
			DateCreate = DateCreate
		};
	}
}
