using JSMR.Application.Common.Search;
using JSMR.Application.Creators.Queries.Search.Contracts;
using JSMR.Infrastructure.Data;
using JSMR.Infrastructure.Data.Repositories.Creators;
using Shouldly;
using SortDirection = JSMR.Application.Common.Search.SortDirection;

namespace JSMR.Tests.Search.Creator;

public class CreatorSearchProviderTests(CreatorSearchProviderFixture fixture) : IClassFixture<CreatorSearchProviderFixture>
{
    private async Task<SearchResult<CreatorSearchItem>> SearchAsync(SearchOptions<CreatorSearchCriteria, CreatorSortField> options)
    {
        AppDbContext context = fixture.DbContext!;
        CreatorSearchProvider provider = new(context);

        return await provider.SearchAsync(options, TestContext.Current.CancellationToken);
    }

    [Fact]
    public async Task Filter_None_Sort_Name_Ascending()
    {
        var options = new SearchOptions<CreatorSearchCriteria, CreatorSortField>()
        {
            SortOptions = [new(CreatorSortField.Name, SortDirection.Ascending)]
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(3);
        result.TotalItems.ShouldBe(3);
        result.Items[0].Name.ShouldBe("Jane Doe");
        result.Items[2].Name.ShouldBe("John Smith");
    }

    [Fact]
    public async Task Filter_None_Sort_Name_Descending()
    {
        var options = new SearchOptions<CreatorSearchCriteria, CreatorSortField>()
        {
            SortOptions = [new(CreatorSortField.Name, SortDirection.Descending)]
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(3);
        result.TotalItems.ShouldBe(3);
        result.Items[0].Name.ShouldBe("John Smith");
        result.Items[2].Name.ShouldBe("Jane Doe");
    }

    [Fact]
    public async Task Filter_None_Sort_Favorite_Descending()
    {
        var options = new SearchOptions<CreatorSearchCriteria, CreatorSortField>()
        {
            SortOptions = [new(CreatorSortField.Favorite, SortDirection.Descending)]
        };

        var result = await SearchAsync(options);

        result.Items[0].Name.ShouldBe("John Doe");
    }

    [Fact]
    public async Task Filter_None_Sort_Blacklisted_Descending()
    {
        var options = new SearchOptions<CreatorSearchCriteria, CreatorSortField>()
        {
            SortOptions = [new(CreatorSortField.Blacklisted, SortDirection.Descending)]
        };

        var result = await SearchAsync(options);

        result.Items[0].Name.ShouldBe("Jane Doe");
    }

    [Fact]
    public async Task Filter_By_Name_Creator_Name()
    {
        var options = new SearchOptions<CreatorSearchCriteria, CreatorSortField>()
        {
            Criteria = new()
            {
                Name = "Jane"
            }
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(1);
        result.TotalItems.ShouldBe(1);
        result.Items.ShouldContain(creatorView => creatorView.Name == "Jane Doe");
    }
}