using Hotline.Api.Controllers;
using Hotline.Share.Dtos.Push;
using Hotline.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using XF.Domain.Exceptions;
using XF.Domain.Repository;

namespace Hotline.Tests.Controller;

public class PushMessageControllerTest
{
    private readonly PushMessageController _pushMessageController;
    private readonly IRepository<User> _userRepository;

    public PushMessageControllerTest(PushMessageController pushMessageController, IRepository<User> userRepository)
    {
        _pushMessageController = pushMessageController;
        pushMessageController.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext()
        };
        _userRepository = userRepository;
    }

    //[Fact]
    public async Task SendMessage_Test()
    {
        try
        {
            await _pushMessageController.SendMessage(new MessageInDto { Content = "123"});
        }
        catch (UserFriendlyException e)
        {
            e.Message.ShouldBe("电话号码和用户Id不能同时为空");
        }

        try
        {
            await _pushMessageController.SendMessage(new MessageInDto());
        }
        catch (UserFriendlyException e)
        {
            e.Message.ShouldBe("内容不能为空");
        }

        var phoneNo = await _userRepository.Queryable()
            .Where(m => m.Name.Contains("测试账号"))
            .Select(m => m.PhoneNo)
            .FirstAsync();

        var inDto = new MessageInDto 
        {
            TelNumbers = phoneNo,
            Content = "单元测试, 输入的手机号码发送的短信"
        };
        // await _pushMessageController.SendMessage(inDto);


        var ids = await _userRepository.Queryable()
            .Where(m => m.Name.Contains("测试账号"))
            .Select(m => m.Id)
            .Take(2)
            .ToListAsync();

        inDto = new MessageInDto
        {
            UserIds = ids,
            Content = "单元测试, 选择的联系人发送的短信"
        };
        await _pushMessageController.SendMessage(inDto);
    }
}

