﻿using System.Collections.Generic;
using Starcounter;

namespace Simplified.Ring3 {
    public partial class SystemUser : Simplified.Ring1.SomebodiesRelation {
        public string Password { get; set; }
        public string PasswordSalt { get; set; }

        /// <summary>
        /// The user name. Also updates the starcounter user.
        /// </summary>
        public string Username {
            get {
                return Name;
            }
            set {
                Name = value;
            }
        }

        /// <summary>
        /// All system user groups that this user belongs to
        /// </summary>
        public IEnumerable<SystemUserGroup> Groups {
            get {
                List<SystemUserGroup> groups = new List<SystemUserGroup>();
                var result = Db.SQL<Simplified.Ring3.SystemUserGroupMember>("SELECT o FROM Simplified.Ring3.SystemUserGroupMember o WHERE o.WhatIs = ? AND o.ToWhat IS Simplified.Ring3.SystemUserGroup", this);
                
                foreach (var item in result) {
                    groups.Add(item.ToWhat as SystemUserGroup);
                }

                return groups;
            }
        }
    }
}
