using System.Linq;

namespace InABox.Core
{
    [UserTracking(typeof(User))]
    [Caption("Security Overrides")]
    [Unrecoverable]
    public class UserSecurityToken : Entity, IPersistent, IRemotable, ILicense<CoreLicense>
    {
        [NullEditor]
        [EntityRelationship(DeleteAction.Cascade)]
        public UserLink User => InitializeField(ref _user, nameof(User));
        private UserLink? _user;

        [ComboLookupEditor(typeof(SecurityRestrictionGenerator))]
        public string Descriptor { get; set; }

        [CheckBoxEditor]
        public bool Enabled { get; set; }

        public override string ToString()
        {
            return Descriptor;
        }

        private class SecurityRestrictionGenerator : LookupGenerator<object>
        {
            public SecurityRestrictionGenerator(object[] items) : base(items)
            {
                foreach (var descriptor in Security.Descriptors.Where(x => x.HasScope(SecurityDescriptorScope.User)))
                    AddValue(descriptor.Code, descriptor.Description);
            }
        }
    }
}