using JSMR.Application.Tags.Queries.Search.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JSMR.Application.Common.Search;




//public abstract class SearchReader2<TItem, TCriteria, TSortField> : ISearchReader<TItem, TCriteria, TSortField>
//    where TSortField : struct, Enum
//{
//    public Task<SearchResult<TItem>> SearchAsync(SearchOptions<TCriteria, TSortField> options, CancellationToken cancellationToken = default)
//    {
//        throw new NotImplementedException();
//    }
//}

//public abstract class SearchReader<TSearchResultItem, TCriteria, TSortField> where TSortField : struct, Enum
//{
//    public async Task<SearchResult<TSearchResultItem>> SearchAsync(SearchOptions<TCriteria, TSortField> options, CancellationToken cancellationToken = default)
//    {
//        IQueryable<TSearchResultItem> baseQuery = GetBaseQuery();
//        IQueryable<TSearchResultItem> filteredQuery = ApplyFilters(baseQuery, options);

//        int total = await filteredQuery.CountAsync(cancellationToken);

//        IOrderedQueryable<TSearchResultItem> orderedQuery = ApplySorting(filteredQuery, options);

//        TSearchResultItem[] items = await orderedQuery
//            .Skip((options.PageNumber - 1) * options.PageSize)
//            .Take(options.PageSize)
//            .ToArrayAsync(cancellationToken);

//        return new SearchResult<TSearchResultItem>()
//        {
//            Items = items,
//            TotalItems = total
//        };
//    }

//    protected abstract IQueryable<TSearchResultItem> GetBaseQuery();
//    protected abstract IQueryable<TSearchResultItem> ApplyFilters(IQueryable<TSearchResultItem> query, SearchOptions<TCriteria, TSortField> options);

//    private IOrderedQueryable<TSearchResultItem> ApplySorting(IQueryable<TSearchResultItem> query, SearchOptions<TCriteria, TSortField> options)
//    {
//        IOrderedQueryable<TSearchResultItem>? ordered = null;

//        for (int i = 0; i < options.SortOptions.Length; i++)
//        {
//            var (field, direction) = (options.SortOptions[i].Field, options.SortOptions[i].Direction);
//            bool isDescending = direction == SortDirection.Descending;

//            IOrderedQueryable<TSearchResultItem> applyFirst(Expression<Func<TSearchResultItem, object>> selector) => isDescending ? query.OrderByDescending(selector) : query.OrderBy(selector);
//            IOrderedQueryable<TSearchResultItem> applyNext(Expression<Func<TSearchResultItem, object>> selector) => isDescending ? ordered!.ThenByDescending(selector) : ordered!.ThenBy(selector);

//            Expression<Func<TSearchResultItem, object>> selector = GetSortExpression(field);

//            ordered = (i == 0) ? applyFirst(selector) : applyNext(selector);
//        }

//        return ordered ?? GetDefaultSortExpression(query);
//    }

//    protected abstract Expression<Func<TSearchResultItem, object>> GetSortExpression(TSortField field);
//    protected abstract IOrderedQueryable<TSearchResultItem> GetDefaultSortExpression(IQueryable<TSearchResultItem> query);
//}