using CarShowroomContracts.AbstractModels;
using CarShowroomDataModels.Views;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarShowroomDatabaseStorage.Entities
{
    [Table("model")]
    [Index(nameof(Name), IsUnique = true)]
    public class Model : IModel
    {
        [Column("model_id")]
        public int Id { get; private set; }
        [Required]
        [Column("model_name")]
        [MaxLength(50)]
        public string Name { get; private set; } = string.Empty;
        [Required]
        [Column("model_price")]
        public int Price { get; private set; }
        [Required]
        [Column("model_make_id")]
        public int MakeId { get; private set; }
        public virtual Make? Make { get; set; }
        public virtual List<Car> Cars { get; set; } = new();

        private Model() { }

        private Model(IModel model)
        {
            Id = model.Id;
            Name = model.Name;
            Price = model.Price;
            MakeId = model.MakeId;
        }

        public static Model? Create(IModel model)
        {
            if (model == null)
                return null;
            return new Model(model);
        }

        public void Update(IModel model)
        {
            if (model == null)
                return;
            Name = model.Name;
            Price = model.Price;
            MakeId = model.MakeId;
        }

        public ModelView GetView()
        {
            ModelView model = new ModelView(this);
            model.MakeName = Make?.Name ?? string.Empty;
            return model;
        }
    }
}