using Dapper;
using Dapper.Contrib.Extensions;

using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Services;

using Microsoft.Extensions.Logging;

using Moq;
using Moq.Dapper;

using System.Data;

namespace FabApprovalWorkerServiceTests;

public class UserServiceTests {
    private static readonly ILogger<UserService> MOCK_LOGGER = Mock.Of<ILogger<UserService>>();

    private static readonly IEnumerable<User> MOCK_USERS = new List<User>() { 
        new User() {
            UserID = 1,
            LoginID = "fred",
            FirstName = "fred",
            LastName = "flintstone",
            Email = "fred.flintstone@email.com",
            IsAdmin = true,
            OOO = false,
            CanViewITAR = true,
            IsManager = false
        },
        new User() {
            UserID = 1,
            LoginID = "barney",
            FirstName = "barney",
            LastName = "rubble",
            Email = "barney.rubble@email.com",
            IsAdmin = false,
            OOO = false,
            CanViewITAR = true,
            IsManager = false
        }
    };

    Mock<IDalService> _mockDalService;

    public UserService _userService;

    [SetUp]
    public void Setup() {
        _mockDalService = new Mock<IDalService>();

        _mockDalService.Setup(d => d.QueryAsync<User>(It.IsAny<string>())).Returns(Task.FromResult(MOCK_USERS));
        _mockDalService.Setup(d => d.UpdateAsync<User>(It.IsAny<ICollection<User>>())).Returns(Task.FromResult(true));

        _userService = new UserService(MOCK_LOGGER, _mockDalService.Object);
    }

    [Test]
    public async Task GetAllUsersShouldReturnMockUsers() {
        Dictionary<string, User> users = await _userService.GetAllUsers();
        Assert.True(users.Count() == 2);
    }

    [Test]
    public void UpdateUserCertificateDataWithNullListShouldThrowException() {
        Assert.ThrowsAsync<ArgumentNullException>(async () => await _userService.UpdateUserCertificationData(null));
    }

    [Test]
    public async Task UpdateUserCertificationDataShouldReturnTrue() {
        bool result = await _userService.UpdateUserCertificationData(MOCK_USERS.ToList());
        Assert.True(result);
    }
}