using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Enums;
using BarDataModels.Models;
using System.ComponentModel.DataAnnotations;

namespace BarDatabaseImplement.Models
{
    public class Order : IOrderModel
    {
        public int Id { get; private set; }

        [Required]
        public int CocktailId { get; private set; }

        public virtual Cocktail Cocktail { get; set; }

        [Required]
        public int ClientId { get; private set; }

        public virtual Client Client { get; set; }

        public int? ImplementerId { get; private set; }

        public virtual Implementer? Implementer { get; set; }

        [Required]
        public int Count { get; private set; }

        [Required]
        public double Sum { get; private set; }

        [Required]
        public OrderStatus Status { get; private set; } = OrderStatus.Undefined;

        [Required]
        public DateTime DateCreate { get; private set; } = DateTime.Now;

        public DateTime? DateImplement { get; private set; }

        public static Order? Create(OrderBindingModel Model)
        {
            if (Model is null)
                return null;

            return new Order()
            {
                Id = Model.Id,
                CocktailId = Model.CocktailId,
                ClientId = Model.ClientId,
                ImplementerId = Model.ImplementerId,
                Count = Model.Count,
                Sum = Model.Sum,
                Status = Model.Status,
                DateCreate = Model.DateCreate,
                DateImplement = Model.DateImplement,
            };
        }

        public void Update(OrderBindingModel Model)
        {
            if (Model is null)
                return;

            ImplementerId = Model.ImplementerId;
            Status = Model.Status;
            DateImplement = Model.DateImplement;
        }

        public OrderViewModel GetViewModel => new()
        {
            Id = Id,
            CocktailId = CocktailId,
            ClientId = ClientId,
            ImplementerId = ImplementerId,
            CocktailName = Cocktail.CocktailName,
            ClientFIO = Client.ClientFIO,
            ClientEmail = Client.Email,
            ImplementerFIO = Implementer?.ImplementerFIO,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
        };
    }
}
