using Microsoft.EntityFrameworkCore;
using PARR.Domain.Constants;
using PARR.Domain.Entities.Base;
using PARR.Domain.Entities.Job;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace PARR.Domain.Entities.Unit
{
    [Table("Units", Schema = DatabaseSchemas.Unit)]
    [Comment("Таблица с ЭК")]
    [Index(nameof(Name), IsUnique = true)]
    public class Unit : IBaseEntity
    {
        [Key]
        public Guid Id { get; set; }

        public DateTimeOffset DateCreated { get; set; }

        [NotMapped]
        public DateTimeOffset? DateModified { get; set; }

        /// <summary>
        /// ЭК
        /// </summary>
        public required string Name { get; set; }

        public DateTimeOffset? LastLogon { get; set; }


        [NotMapped]
        public BaseFields? BaseFields
        {
            get
            {

                if (UnitValues.Any())
                {
                    var result = new BaseFields();

                    result.IP = UnitValues.FirstOrDefault(t => t.Field?.AihitName == "IP_АДРЕС")?.Value?.Value;
                    result.ResponseArea = UnitValues.FirstOrDefault(t => t.Field?.AihitName == "ЗО_РГ")?.Value?.Value;
                    result.WorkGroup = UnitValues.FirstOrDefault(t => t.Field?.AihitName == "РАБОЧАЯ_ГР_ОТВ_ЗА_ЭК")?.Value?.Value;
                    result.Status = UnitValues.FirstOrDefault(t => t.Field?.AihitName == "СТАТУС")?.Value?.Value;
                    result.NotUnique = UnitValues.FirstOrDefault(t => t.Field?.AihitName == "НЕУНИКАЛЬНЫЙ_ЭК")?.Value?.Value;

                    return result;
                }

                return null;
            }
        }

        public ICollection<UnitInValue> UnitValues { get; set; } = new HashSet<UnitInValue>();


        /// <summary>
        /// Получить всех родителей
        /// </summary>
        [InverseProperty(nameof(UnitInUnit.ChildUnit))]
        public ICollection<UnitInUnit> ParentUnits { get; set; } = new HashSet<UnitInUnit>();

        /// <summary>
        /// Получить детей
        /// </summary>
        [InverseProperty(nameof(UnitInUnit.ParentUnit))]
        public ICollection<UnitInUnit> ChildUnits { get; set; } = new HashSet<UnitInUnit>();


        /// <summary>
        /// Связь с шаблонами. Один ко многим
        /// </summary>
        public ICollection<Template> Templates { get; set; } = new HashSet<Template>();

        /// <summary>
        /// К одному шаблону привязано несколько ЭК(Груповой тип работы). Многие ко многим
        /// </summary>
        public ICollection<UnitsInTemplate> TemplatesInUnit { get; set; } = new HashSet<UnitsInTemplate>();


        public UnitKiiUnit? UnitKii { get; set; }
    }


    public class BaseFields
    {
        public string? IP { get; set; }

        public string? ResponseArea { get; set; }

        public string? WorkGroup { get; set; }

        public string? Status { get; set; }

        public string? NotUnique { get; set; }
    }
}
