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

namespace JSMR.Tests.Search.Tag;

public class TagSearchProviderTests(TagSearchProviderFixture fixture) : IClassFixture<TagSearchProviderFixture>
{
    private async Task<SearchResult<TagSearchItem>> SearchAsync(SearchOptions<TagSearchCriteria, TagSortField> options)
    {
        AppDbContext context = fixture.DbContext!;
        TagSearchProvider provider = new(context);

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

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

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(3);
        result.TotalItems.ShouldBe(3);
        result.Items[0].Name.ShouldBe("OL");
        result.Items[2].Name.ShouldBe("ツンデレ");
    }

    [Fact]
    public async Task Filter_None_Sort_EnglishName()
    {
        var options = new SearchOptions<TagSearchCriteria, TagSortField>()
        {
            SortOptions = [new(TagSortField.EnglishName, SortDirection.Ascending)]
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(3);
        result.TotalItems.ShouldBe(3);
        result.Items[0].EnglishName.ShouldBe("Heartwarming");
        result.Items[2].EnglishName.ShouldBe("Tsundere");
    }

    [Fact]
    public async Task Filter_By_Name_Tag_Name()
    {
        var options = new SearchOptions<TagSearchCriteria, TagSortField>()
        {
            Criteria = new()
            {
                Name = "ほの"
            }
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(1);
        result.TotalItems.ShouldBe(1);
        result.Items.ShouldContain(tagView => tagView.Name == "ほのぼの");
    }

    [Fact]
    public async Task Filter_By_Name_English_Tag_Name()
    {
        var options = new SearchOptions<TagSearchCriteria, TagSortField>()
        {
            Criteria = new()
            {
                Name = "Heart"
            }
        };

        var result = await SearchAsync(options);

        result.Items.Length.ShouldBe(1);
        result.TotalItems.ShouldBe(1);
        result.Items.ShouldContain(tagView => tagView.EnglishName == "Heartwarming");
    }

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

        var result = await SearchAsync(options);

        result.Items[0].EnglishName.ShouldBe("Heartwarming");
    }

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

        var result = await SearchAsync(options);

        result.Items[0].EnglishName.ShouldBe("Tsundere");
    }
}