using Hotline.Api.Controllers;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Identity.Accounts;
using Hotline.Identity.Roles;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.Snapshot;
using Hotline.Settings;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Snapshot.IRepository;
using Hotline.ThirdAccountDomainServices;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Authentications;
using XF.Domain.Cache;
using XF.Domain.Repository;

namespace Hotline.Tests.Application;
public class InviteCodeApplicationTest : TestBase
{
    private readonly IInviteCodeApplication _inviteCodeApplication;
    private readonly ISnapshotApplication _snapshotApplication;
    private readonly IInviteCodeRepository _inviteCodeRepository;
    private readonly ISessionContext _sessionContext;
    private readonly IInviteCodeRecordRepository _inviteCodeRecordRepository;
    private readonly ICitizenRepository _citizenRepository;

    public InviteCodeApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, IInviteCodeApplication inviteCodeApplication, ISnapshotApplication snapshotApplication, IInviteCodeRepository inviteCodeRepository, ISessionContext sessionContext, IInviteCodeRecordRepository inviteCodeRecordRepository, ITypedCache<SystemSetting> cacheSettingData, ThirdAccounSupplierFactory thirdAccountDomainFactory, ICitizenRepository citizenRepository, IServiceProvider serviceProvider) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService, thirdAccountRepository, cacheSettingData, thirdAccountDomainFactory, serviceProvider)
    {
        _inviteCodeApplication = inviteCodeApplication;
        _snapshotApplication = snapshotApplication;
        this._inviteCodeRepository = inviteCodeRepository;
        _sessionContext = sessionContext;
        _inviteCodeRecordRepository = inviteCodeRecordRepository;
        _citizenRepository = citizenRepository;
    }

    [Fact]
    public async Task InviteCode_Test()
    {
        var inDto = new AddInviteCodeInDto
        {
            OrgName = "测试部门" + DateTime.Now.ToString("ss"),
            BeginCode = 100,
            EndCode = 200,
        };

        var id = await _inviteCodeApplication.AddInviteCodeAsync(inDto);

        var items = _inviteCodeApplication.GetInviteCodeItems().ToList();
        items.Count.ShouldNotBe(0);
        await _inviteCodeRepository.RemoveAsync(id);

        SetWeiXin();

        await _citizenRepository.Updateable()
            .SetColumns(m => m.InvitationCode, null)
            .Where(m => m.Id == _sessionContext.UserId)
            .ExecuteCommandAsync();
        await _inviteCodeRecordRepository.Removeable().Where(m => m.InviteCode == "110").ExecuteCommandAsync();
        await _snapshotApplication.SaveInvitationCodeAsync(new SaveInvitationCodeInDto { InvitationCode = "110"});

        var statics = await _inviteCodeApplication.GetInviteCodeStatisticAsync(new GetInviteCodeStatisticInDto 
        {
            StartTime = DateTime.Now.AddDays(-1),
            EndTime = DateTime.Now
        }).ToListAsync();
        statics.Count.ShouldNotBe(0);
    }
}
