﻿// ----------------------------------------------------------------------------------------------------------------------
// Author: Tanveer Yousuf (@tanveery)
// ----------------------------------------------------------------------------------------------------------------------
// Copyright © Ejyle Technologies (P) Ltd. All rights reserved.
// Licensed under the MIT license. See the LICENSE file in the project's root directory for complete license information.
// ----------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using Ejyle.DevAccelerate.Core.EF;
using Ejyle.DevAccelerate.Identity.AspNet.EF.Tenants;
using Ejyle.DevAccelerate.Identity.AspNet.EF.UserActivities;
using Ejyle.DevAccelerate.Identity.AspNet.EF.UserSessions;
using Ejyle.DevAccelerate.Identity.AspNet.UserAgreements;

namespace Ejyle.DevAccelerate.Identity.AspNet.EF.UserAgreements
{
    public class DaUserAgreementRepository : DaUserAgreementRepository<DaUser, DaRole, DaUserRole, DaUserLogin, DaUserClaim, DaTenant, DaTenantUser, DaUserSession, DaUserActivityCategory, DaUserActivity, DaUserAgreement, DaUserAgreementVersion, DaUserAgreementVersionUser, DaAspNetIdentityDbContext>
    {
        public DaUserAgreementRepository(DaAspNetIdentityDbContext dbContext)
            : base(dbContext)
        { }
    }

    public class DaUserAgreementRepository<TUser, TRole, TUserRole, TUserLogin, TUserClaim, TTenant, TTenantUser, TUserSession, TUserActivityCategory, TUserActivity, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser, TDbContext>
        : DaUserAgreementRepository<int, int?, TUser, TRole, TUserRole, TUserLogin, TUserClaim, TTenant, TTenantUser, TUserSession, TUserActivityCategory, TUserActivity, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser, TDbContext>
        where TUser : DaUser<int, int?, TUserLogin, TUserRole, TUserClaim>
        where TRole : DaRole<int, TUserRole>, new()
        where TUserLogin : DaUserLogin<int>
        where TUserRole : DaUserRole<int>, new()
        where TUserClaim : DaUserClaim<int>
        where TTenant : DaTenant<int, int?, TTenantUser>
        where TTenantUser : DaTenantUser<int, int?, TTenant, TUser>
        where TUserSession : DaUserSession<int>
        where TUserActivityCategory : DaUserActivityCategory<int, int?, TUserActivity>
        where TUserActivity : DaUserActivity<int, int?, TUserActivityCategory>
        where TUserAgreement : DaUserAgreement<int, int?, TUserAgreementVersion>
        where TUserAgreementVersion : DaUserAgreementVersion<int, int?, TUserAgreement>
        where TUserAgreementVersionUser : DaUserAgreementVersionUser<int>
        where TDbContext : DaAspNetIdentityDbContext<int, int?, TUser, TRole, TUserLogin, TUserRole, TUserClaim, TTenant, TTenantUser, TUserSession, TUserActivityCategory, TUserActivity, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser>
    {
        public DaUserAgreementRepository(TDbContext context)
            : base(context)
        { }
    }

    public class DaUserAgreementRepository<TKey, TNullableKey, TUser, TRole, TUserRole, TUserLogin, TUserClaim, TTenant, TTenantUser, TUserSession, TUserActivityCategory, TUserActivity, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser, TDbContext>
         : DaEntityRepositoryBase<TKey, TTenant, TDbContext>, IDaUserAgreementRepository<TKey, TNullableKey, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser>
         where TKey : IEquatable<TKey>
         where TUser : DaUser<TKey, TNullableKey, TUserLogin, TUserRole, TUserClaim>
         where TRole : DaRole<TKey, TUserRole>, new()
         where TUserLogin : DaUserLogin<TKey>
         where TUserRole : DaUserRole<TKey>, new()
         where TUserClaim : DaUserClaim<TKey>
         where TTenant : DaTenant<TKey, TNullableKey, TTenantUser>
         where TTenantUser : DaTenantUser<TKey, TNullableKey, TTenant, TUser>
         where TUserSession : DaUserSession<TKey>
         where TUserActivityCategory : DaUserActivityCategory<TKey, TNullableKey, TUserActivity>
         where TUserActivity : DaUserActivity<TKey, TNullableKey, TUserActivityCategory>
         where TUserAgreement : DaUserAgreement<TKey, TNullableKey, TUserAgreementVersion>
         where TUserAgreementVersion : DaUserAgreementVersion<TKey, TNullableKey, TUserAgreement>
         where TUserAgreementVersionUser : DaUserAgreementVersionUser<TKey>
         where TDbContext : DaAspNetIdentityDbContext<TKey, TNullableKey, TUser, TRole, TUserLogin, TUserRole, TUserClaim, TTenant, TTenantUser, TUserSession, TUserActivityCategory, TUserActivity, TUserAgreement, TUserAgreementVersion, TUserAgreementVersionUser>
    {
        public DaUserAgreementRepository(TDbContext dbContext)
            : base(dbContext)
        { }

        public Task CreateAsync(TUserAgreement userAgreement)
        {
            DbContext.UserAgreements.Add(userAgreement);
            return DbContext.SaveChangesAsync();
        }

        public Task CreateAsync(TUserAgreementVersionUser userAgreementVersionUser)
        {
            throw new NotImplementedException();
        }

        public Task<TUserAgreement> FindByIdAsync(TKey userAgreementId)
        {
            return DbContext.UserAgreements.Where(m => m.Id.Equals(userAgreementId)).SingleOrDefaultAsync();
        }

        public Task<TUserAgreement> FindByKeyAsync(string userAgreementKey)
        {
            return DbContext.UserAgreements.Where(m => m.UserAgreementKey == userAgreementKey).SingleOrDefaultAsync();
        }

        public Task<TUserAgreement> FindByVersionIdAsync(TKey userAgreementVersionId)
        {
            throw new NotImplementedException();
        }

        public Task<TUserAgreementVersionUser> FindCurrentUserAgreementVersionAsync(string key, TKey userId)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(TUserAgreement userAgreement)
        {
            DbContext.Entry(userAgreement).State = EntityState.Modified;
            return DbContext.SaveChangesAsync();
        }
    }
}
