﻿using System;
using Raven.Client;

namespace RpgRooms.Website
{
    public static partial class Functions
    {
        public static IMember CreateMember(
            this IDocumentSession documentSession,
            string authType,
            string authId,
            string alias,
            string emailAddress)
        {
            documentSession.Ensure("documentSession");
            authType.Ensure("authType");
            authId.Ensure("authId");
            alias.Ensure("alias");
            emailAddress.Ensure("emailAddress");

            var member = GetMemberByAuth(documentSession, authType, authId);
            if (member != null)
                throw new Exception(Strings.MembershipAlreadyExists(authType, authId));

            member = GetMemberByAlias(documentSession, alias);
            if (member != null)
                throw new Exception(Strings.NotAvailable("alias", alias));

            member = GetMemberByEmailAddress(documentSession, emailAddress);
            if (member != null)
                throw new Exception(Strings.NotAvailable("email address", emailAddress));

            member = new Member
            {
                AuthType = authType,
                AuthId = authId,
                Alias = alias,
                EmailAddress = emailAddress
            };

            documentSession.Store(member);

            return member;
        }
    }
}