using FluentAssertions;
using Moq;
using ColaFlow.Modules.ProjectManagement.Application.Commands.AssignStory;
using ColaFlow.Modules.ProjectManagement.Domain.Aggregates.ProjectAggregate;
using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using ColaFlow.Modules.ProjectManagement.Domain.ValueObjects;
using ColaFlow.Modules.ProjectManagement.Domain.Exceptions;

namespace ColaFlow.Application.Tests.Commands.AssignStory;

public class AssignStoryCommandHandlerTests
{
    private readonly Mock<IProjectRepository> _projectRepositoryMock;
    private readonly Mock<IUnitOfWork> _unitOfWorkMock;
    private readonly AssignStoryCommandHandler _handler;

    public AssignStoryCommandHandlerTests()
    {
        _projectRepositoryMock = new Mock<IProjectRepository>();
        _unitOfWorkMock = new Mock<IUnitOfWork>();
        _handler = new AssignStoryCommandHandler(_projectRepositoryMock.Object, _unitOfWorkMock.Object);
    }

    [Fact]
    public async Task Should_Assign_Story_Successfully()
    {
        // Arrange
        var userId = UserId.Create();
        var assigneeId = UserId.Create();
        var project = Project.Create(TenantId.Create(Guid.NewGuid()), "Test Project", "Description", "TST", userId);
        var epic = project.CreateEpic("Test Epic", "Epic Description", userId);
        var story = epic.CreateStory("Test Story", "Description", TaskPriority.Medium, userId);

        _projectRepositoryMock
            .Setup(x => x.GetProjectWithStoryAsync(story.Id, It.IsAny<CancellationToken>()))
            .ReturnsAsync(project);

        var command = new AssignStoryCommand
        {
            StoryId = story.Id.Value,
            AssigneeId = assigneeId.Value
        };

        // Act
        var result = await _handler.Handle(command, CancellationToken.None);

        // Assert
        result.Should().NotBeNull();
        result.AssigneeId.Should().Be(assigneeId.Value);
        story.AssigneeId.Should().Be(assigneeId);

        _projectRepositoryMock.Verify(x => x.Update(project), Times.Once);
        _unitOfWorkMock.Verify(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
    }

    [Fact]
    public async Task Should_Fail_When_Story_Not_Found()
    {
        // Arrange
        var storyId = StoryId.Create();
        _projectRepositoryMock
            .Setup(x => x.GetProjectWithStoryAsync(storyId, It.IsAny<CancellationToken>()))
            .ReturnsAsync((Project?)null);

        var command = new AssignStoryCommand
        {
            StoryId = storyId.Value,
            AssigneeId = Guid.NewGuid()
        };

        // Act
        Func<Task> act = async () => await _handler.Handle(command, CancellationToken.None);

        // Assert
        await act.Should().ThrowAsync<NotFoundException>()
            .WithMessage("*Story*");
    }

    [Fact]
    public async Task Should_Reassign_Story_To_Different_User()
    {
        // Arrange
        var userId = UserId.Create();
        var firstAssignee = UserId.Create();
        var secondAssignee = UserId.Create();
        var project = Project.Create(TenantId.Create(Guid.NewGuid()), "Test Project", "Description", "TST", userId);
        var epic = project.CreateEpic("Test Epic", "Epic Description", userId);
        var story = epic.CreateStory("Test Story", "Description", TaskPriority.Medium, userId);

        // First assign
        story.AssignTo(firstAssignee);

        _projectRepositoryMock
            .Setup(x => x.GetProjectWithStoryAsync(story.Id, It.IsAny<CancellationToken>()))
            .ReturnsAsync(project);

        var command = new AssignStoryCommand
        {
            StoryId = story.Id.Value,
            AssigneeId = secondAssignee.Value
        };

        // Act
        var result = await _handler.Handle(command, CancellationToken.None);

        // Assert
        result.Should().NotBeNull();
        result.AssigneeId.Should().Be(secondAssignee.Value);
        story.AssigneeId.Should().Be(secondAssignee);
    }
}
