using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;

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

        [Required]
        public int SushiId { get; private set; }
        
        [Required]
        public int ClientId { get; private set; }
        public int? ImplementerId { get; private set; } = null;
        
        public string SushiName { get; set; } = string.Empty;

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

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

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

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

        public DateTime? DateImplement { get; private set; }

        public virtual Sushi Sushi { get; set; }
        
        public virtual Client Client { get; set; }
        
        public virtual Implementer? Implementer { get; private set; }

        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }

            return new Order
            {
                Id = model.Id,
                SushiId = model.SushiId,
                SushiName = model.SushiName,
                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 == null)
            {
                return;
            }

            SushiId = model.SushiId;
            SushiName = model.SushiName;
            ClientId = model.ClientId;
            ImplementerId = model.ImplementerId;
            Count = model.Count;
            Sum = model.Sum;
            Status = model.Status;
            DateCreate = model.DateCreate;
            DateImplement = model.DateImplement;
        }

        public OrderViewModel GetViewModel { get
        {
            var context = new SushiBarDatabase();
            return new OrderViewModel
            {
                Id = Id,
                ClientId = ClientId,
                SushiId = SushiId,
                Count = Count,
                DateCreate = DateCreate,
                DateImplement = DateImplement,
                ImplementerId = ImplementerId,
                Sum = Sum,
                Status = Status,
                ClientFio = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFio ?? string.Empty,
                SushiName = context.Sushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty,
                ImplementerFio = context.Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFio ?? string.Empty,
            };
        } }
    }
}
