using ColaFlow.Modules.Mcp.Domain.Services;
using FluentAssertions;

namespace ColaFlow.Modules.Mcp.Tests.Domain;

public class DiffPreviewServiceTests
{
    private readonly DiffPreviewService _service;

    public DiffPreviewServiceTests()
    {
        _service = new DiffPreviewService();
    }

    private class TestEntity
    {
        public Guid Id { get; set; }
        public string Title { get; set; } = string.Empty;
        public string? Description { get; set; }
        public int Priority { get; set; }
        public DateTime CreatedAt { get; set; }
    }

    [Fact]
    public void GenerateCreateDiff_WithValidEntity_ShouldCreateDiff()
    {
        var entity = new TestEntity
        {
            Id = Guid.NewGuid(),
            Title = "Test Entity",
            Description = "Test Description",
            Priority = 1,
            CreatedAt = DateTime.UtcNow
        };

        var diff = _service.GenerateCreateDiff("TestEntity", entity, "TEST-001");

        diff.Should().NotBeNull();
        diff.Operation.Should().Be("CREATE");
        diff.EntityType.Should().Be("TestEntity");
        diff.EntityKey.Should().Be("TEST-001");
        diff.EntityId.Should().BeNull();
        diff.BeforeData.Should().BeNull();
        diff.AfterData.Should().NotBeNullOrEmpty();
        diff.AfterData.Should().Contain("Test Entity");
    }

    [Fact]
    public void GenerateUpdateDiff_WithChangedFields_ShouldDetectChanges()
    {
        var entityId = Guid.NewGuid();
        var createdAt = DateTime.UtcNow;

        var beforeEntity = new TestEntity
        {
            Id = entityId,
            Title = "Original Title",
            Description = "Original Description",
            Priority = 1,
            CreatedAt = createdAt
        };

        var afterEntity = new TestEntity
        {
            Id = entityId,
            Title = "Updated Title",
            Description = "Updated Description",
            Priority = 1,
            CreatedAt = createdAt
        };

        var diff = _service.GenerateUpdateDiff("TestEntity", entityId, beforeEntity, afterEntity, "TEST-003");

        diff.Should().NotBeNull();
        diff.Operation.Should().Be("UPDATE");
        diff.ChangedFields.Should().HaveCount(2);
        diff.ChangedFields.Should().Contain(f => f.FieldName == "Title");
        diff.ChangedFields.Should().Contain(f => f.FieldName == "Description");
    }

    [Fact]
    public void GenerateHtmlDiff_WithUpdateOperation_ShouldGenerateTable()
    {
        var entityId = Guid.NewGuid();
        var beforeEntity = new TestEntity { Id = entityId, Title = "Before", Priority = 1 };
        var afterEntity = new TestEntity { Id = entityId, Title = "After", Priority = 2 };
        var diff = _service.GenerateUpdateDiff("TestEntity", entityId, beforeEntity, afterEntity, "TEST-003");

        var html = _service.GenerateHtmlDiff(diff);

        html.Should().NotBeNullOrEmpty();
        html.Should().Contain("UPDATE");
        html.Should().Contain("TestEntity");
        html.Should().Contain("<table");
        html.Should().Contain("diff-removed");
        html.Should().Contain("diff-added");
    }
}
