using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FtxDemo.Data {
    public static class StoryExtensions {

        public static IEnumerable<Story> FulltextSearch(this DbSet<Story> stories, string query) {
            // Validate arguments
            if (stories == null) throw new ArgumentNullException(nameof(stories));
            if (query == null) throw new ArgumentNullException(nameof(query));
            if (string.IsNullOrWhiteSpace(query)) throw new ArgumentException("Value cannot be empty or whitespace only string.", nameof(query));

            // Create FTX query
            var ftxQuery = FtxInterceptor.CreateQuery(query, addPrefix: false);

            // Execute SQL query
            var r = stories.SqlQuery("SELECT S.* FROM Stories AS S INNER JOIN CONTAINSTABLE(Stories, *, @p0) AS R ON R.[KEY] = S.Id ORDER BY R.RANK DESC", ftxQuery);
            return r.AsEnumerable();
        }

    }
}
