using AyCode.Core.Enums;
using AyCode.Core.Tests.TestModels;

namespace AyCode.Services.Server.Tests.SignalRs.SignalRDatasources;

public abstract partial class SignalRDataSourceTestBase<TDataSource, TIList>
{
    #region SaveChanges Tests

    [TestMethod]
    public virtual async Task SaveChanges_SavesTrackedItems()
    {
        var dataSource = CreateDataSource(_client, _crudTags);

        dataSource.Add(new TestOrderItem_All_True { Id = 101, ProductName = "Item 1" });
        dataSource.Add(new TestOrderItem_All_True { Id = 102, ProductName = "Item 2" });

        var unsaved = await dataSource.SaveChanges();

        Assert.AreEqual(0, unsaved.Count);
        Assert.AreEqual(0, dataSource.GetTrackingItems().Count);
    }

    [TestMethod]
    public virtual async Task SaveChangesAsync_ClearsTracking()
    {
        var dataSource = CreateDataSource(_client, _crudTags);
        dataSource.Add(new TestOrderItem_All_True { Id = 103, ProductName = "Item 3" });

        await dataSource.SaveChangesAsync();

        Assert.AreEqual(0, dataSource.GetTrackingItems().Count);
    }

    [TestMethod]
    public virtual async Task SaveItem_ById_SavesSpecificItem()
    {
        var dataSource = CreateDataSource(_client, _crudTags);

        dataSource.Add(new TestOrderItem_All_True { Id = 201, ProductName = "Specific" });
        var result = await dataSource.SaveItem(201);

        Assert.AreEqual("Specific", result.ProductName);
        Assert.AreEqual(0, dataSource.GetTrackingItems().Count);
    }

    [TestMethod]
    public virtual async Task SaveItem_WithTrackingState_SavesCorrectly()
    {
        var dataSource = CreateDataSource(_client, _crudTags);
        await dataSource.LoadDataSource();

        var item = dataSource[0];
        var result = await dataSource.SaveItem(item, TrackingState.Update);

        Assert.IsNotNull(result);
    }

    #endregion
}
