using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace CaseAccountingDataBaseImplement.Models
{
    public class Specialization : ISpecializationModel
    {
        public int Id { get; set; }

        [Required]
        public string Name { get; set; } = string.Empty;

        [Required]
        public int UserId { get; set; }
        public virtual User User { get; set; } = new();

        [ForeignKey("SpecializationId")]
        public virtual List<Lawyer> Lawyers { get; set; } = new();

        [ForeignKey("SpecializationId")]
        public virtual List<Case> Cases { get; set; } = new();

        public static Specialization? Create (CaseAccountingDatabase context, SpecializationBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Specialization()
            {
                Id = model.Id,
                Name = model.Name,
                UserId = model.UserId,
                User = context.Users.FirstOrDefault(x => x.Id == model.UserId) ?? throw new Exception("User not found"),
            };
        }

        public void Update(SpecializationBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
        }

        public SpecializationViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            UserId = UserId
        };
    }
}
