using CarServiceContracts.BindingModels;
using CarServiceContracts.Models;
using CarServiceContracts.ViewModels;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace CarServiceDatabase.Models
{
	public class WorkPayment : IWorkPaymentModel
	{
		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 WorkInRequestId { get; private set; }
		public virtual WorkInRequest WorkInRequest { get; set; } = new();
		public static WorkPayment? Create(CarServiceDbContext context, WorkPaymentBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}
			return new()
			{
				DatePayment = model.DatePayment,
				Sum = model.Sum,
				WorkInRequest = context.WorksInRequest.First(x => x.Id == model.WorkInRequestId) 
			};
		}
		public void Update(CarServiceDbContext context, WorkPaymentBindingModel? model)
		{
			if (model == null)
			{
				return;
			}
			DatePayment = model.DatePayment;
			Sum = model.Sum;
			WorkInRequest = context.WorksInRequest.First(x => x.Id == model.WorkInRequestId);
		}
		public WorkPaymentViewModel GetViewModel => new()
		{
			Id = Id,
			DatePayment = DatePayment,
			Sum = Sum,
			WorkInRequestId = WorkInRequestId
		};
	}
}