using System;
using Xunit;
using Newtonsoft.Json.Linq;
using System.Linq;
using FluentAssertions;
using System.Collections.Generic;

namespace raven_integration
{

    public class SearchOps
    {

        [Fact]
        public async void PhraseOnlySearchShouldReturnCorrectResultsInOrder()
        {
            const string TEST_SEARCH_PHRASE = "simple dogs";

            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("Search NOTES Test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in notes: The quick brown and simple fox jumped over the six lazy dogs!";

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNotesId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE FIRST TEST USER WITH PHRASE IN NAME
            D = new JObject();
            D.name = Util.Uniquify("Search NAME DOGS simple Test User");
            D.notes = "This user has the match in it's name";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNameId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE A SECOND TEST USER WITH PHRASE IN NOTES
            D = new JObject();
            D.name = Util.Uniquify("Search NOTES Test User");
            D.notes = "This user has the match simple dogs in its notes";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNotesId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE A SECOND WIDGET
            D = new JObject();
            D.name = Util.Uniquify("Search NAME simple as in dogs Test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This Widget matches in name";

            a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNameId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE A THIRD WIDGET
            D = new JObject();
            D.name = Util.Uniquify("Search NO-MATCH THIRD Test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This Widget should not be returned in the search as it only contains a single keyword in the name not both";

            a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchNothingWidgetId = a.ObjectResponse["data"]["id"].Value<long>();


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(3);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId");
            MatchingIdList.Should().Contain(MatchWidgetInNameId, "ShouldContainMatchWidgetInNameId");
            MatchingIdList.Should().Contain(MatchUserInNotesId, "ShouldContainMatchUserInNotesId");
            MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId");
            MatchingIdList.Should().NotContain(MatchNothingWidgetId, "ShouldNotContainMatchNothingWidgetId");

            //Assert the order (roughly, this is kind of a waste of time, either the code is sorting or not, it's not going to change)       
            //first item must be a widget
            a.ObjectResponse["data"]["searchResults"][0]["type"].Value<int>().Should().Be(2);

            //final item must be a user
            a.ObjectResponse["data"]["searchResults"][MatchingIdList.Count - 1]["type"].Value<int>().Should().Be(3);


            //FULL BODY SEARCH RIGHTS
            //First up test a full record search returns no results due to insufficient rights
            //even though the record exists
            //Just re-run the above search exactly but with a no rights to full User or Widget role instead

            //Only BizAdmin* roles can read a full user record but anyone should be able to see names
            //This search should return zero items
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("SubContractorLimited"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().Be(0, "User with no rights should not see any results in body search");


            //NAME ONLY SEARCH SHOULD WORK WITH NO RIGHTS TO READ FULL RECORD
            //repeat same search but with nameOnly = true and should return at two records at least but not any of the body ones
            SearchParameters = new JObject();
            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = true;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("SubContractorLimited"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(2);
            //Check that list does *not* include the notes only records
            MatchingIdList = new List<long>();
            v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }
            MatchingIdList.Should().NotContain(MatchWidgetInNotesId, "ShouldNotContainMatchWidgetInNotesId");
            MatchingIdList.Should().Contain(MatchWidgetInNameId, "ShouldContainMatchWidgetInNameId");
            MatchingIdList.Should().NotContain(MatchUserInNotesId, "ShouldContainMatchUserInNotesId");
            MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId");
            MatchingIdList.Should().NotContain(MatchNothingWidgetId, "ShouldNotContainThirdWidget");

        }//eot



        [Fact]
        public async void WildCardStartsWithSearchShouldWork()
        {
            const string TEST_SEARCH_PHRASE = "hap* goose";

            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("Wildcard startswith search test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in notes: The quick brown and hapless goose";

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNotesId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE FIRST TEST USER WITH PHRASE IN NAME
            D = new JObject();
            D.name = Util.Uniquify("Wildcard startswith search NAME happy goose Test User");
            D.notes = "This user has the match in it's name";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNameId = a.ObjectResponse["data"]["id"].Value<long>();


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(2);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId");
            MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId");

        }//eot


        [Fact]
        public async void WildCardEndsWithSearchShouldWork()
        {
            const string TEST_SEARCH_PHRASE = "goose *act";

            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("Wildcard endswith search test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in notes: react to the goose";

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNotesId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE FIRST TEST USER WITH PHRASE IN NAME
            D = new JObject();
            D.name = Util.Uniquify("Wildcard endswith search NAME goose exact Test User");
            D.notes = "This user has the match in it's name";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNameId = a.ObjectResponse["data"]["id"].Value<long>();


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(2);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId");
            MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId");

        }//eot




        [Fact]
        public async void WildCardContainsSearchShouldWork()
        {
            const string TEST_SEARCH_PHRASE = "*cast* goose";

            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("Wildcard contains search test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in notes: broadcasting to the goose";

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNotesId = a.ObjectResponse["data"]["id"].Value<long>();

            //CREATE FIRST TEST USER WITH PHRASE IN NAME
            D = new JObject();
            D.name = Util.Uniquify("Wildcard contains search NAME goose elcastro Test User");
            D.notes = "This user has the match in it's name";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNameId = a.ObjectResponse["data"]["id"].Value<long>();


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(2);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId");
            MatchingIdList.Should().Contain(MatchUserInNameId, "ShouldContainMatchUserInNameId");

        }//eot


        [Fact]
        public async void TagSearchShouldWork()
        {
            const string TEST_SEARCH_PHRASE = "element* aardvark";

            // //CREATE A TAG
            // dynamic D = new JObject();
            // D.name = Util.Uniquify("TAGSEARCH");
            // ApiResponse a = await Util.PostAsync("Tag", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            // Util.ValidateDataReturnResponseOk(a);
            // long TagId = a.ObjectResponse["data"]["id"].Value<long>();


            var SearchTagPhrase = Util.Uniquify("TAGSEARCH");
            //Tags 
            dynamic TagsArray = new JArray();
            TagsArray.Add(SearchTagPhrase);

            //w.tags = InclusiveTagsArray;

            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("TAG search test WIDGET TAG AND PHRASE");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in notes and tag: elementary my dear aardvark";
            D.tags = TagsArray;

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInNotesId = a.ObjectResponse["data"]["id"].Value<long>();




            //CREATE A WIDGET WITH TAG BUT NOT SEARCH PHRASE
            D = new JObject();
            D.name = Util.Uniquify("TAG search test WIDGET TAG ONLY");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "This record will match in tag but no search phrase";
            D.tags = TagsArray;

            a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long NoPhraseMatchWidgetInTagId = a.ObjectResponse["data"]["id"].Value<long>();



            //CREATE FIRST TEST USER WITH PHRASE IN NAME BUT NO TAG
            D = new JObject();
            D.name = Util.Uniquify("Wildcard contains search NAME elementary aardvark Test User");
            D.notes = "This user has the match in it's name but no tag match";
            D.ownerId = 1L;
            D.active = true;
            D.login = Util.Uniquify("LOGIN");
            D.password = Util.Uniquify("PASSWORD");
            D.roles = 0;//norole
            D.localeId = 1;//random locale
            D.userType = 3;//non scheduleable

            a = await Util.PostAsync("User", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchUserInNameId = a.ObjectResponse["data"]["id"].Value<long>();


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE + " " + SearchTagPhrase;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type

            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(1);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInNotesId, "ShouldContainMatchWidgetInNotesId");
            MatchingIdList.Should().NotContain(MatchUserInNameId, "ShouldNotContainMatchUserInNameId");
            MatchingIdList.Should().NotContain(NoPhraseMatchWidgetInTagId, "ShouldNotContainNoPhraseMatchWidgetInTagId");

        }//eot



        [Fact]
        public async void ConstrainedBigDataSearchShouldHonourMaxResultsAndBeRelativelyFast()
        {
            //THIS test is a bit different in that it relies partly on the big dataset for testing
            //so it has different paths depending upon if it's testing against the big data or not
            const string TEST_SEARCH_PHRASE = "et*";

            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 1000;//default is 500

            var watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            ApiResponse a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            watch.Stop();

            var TimeToSearch = watch.ElapsedMilliseconds;

            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            var ResultCount = ((JArray)a.ObjectResponse["data"]["searchResults"]).Count;
            //assert it's not unbounded
            ResultCount.Should().BeLessOrEqualTo(1000);

            if (ResultCount > 999)
            {
                //assert the TotalResultsFound is greater than the results returned 
                var TotalResultsFound = a.ObjectResponse["data"]["totalResultsFound"].Value<long>();
                //assert it's not unbounded
                TotalResultsFound.Should().BeGreaterThan(ResultCount);
            }
            //5456 ms is the longest I've seen in initial testing with all 1000 results so setting 10% above
            TimeToSearch.Should().BeLessThan(6000, "Constrained big data search should not be too slow");

        }//eot


        [Fact]
        public async void UnboundBigDataSearchShouldBeRelativelyFast()
        {
            //THIS test is a bit different in that it relies partly on the big dataset for testing
            //so it has different paths depending upon if it's testing against the big data or not
            const string TEST_SEARCH_PHRASE = "et*";

            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();

            SearchParameters.phrase = TEST_SEARCH_PHRASE;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;//0=return all results

            var watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            ApiResponse a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            watch.Stop();

            var TimeToSearch = watch.ElapsedMilliseconds;

            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            var ResultCount = ((JArray)a.ObjectResponse["data"]["searchResults"]).Count;


            //Set this time based on testing.  52385 Slowest run plus 10%
            TimeToSearch.Should().BeLessThan(57623, "Unconstrained big data search should not be too slow");


        }//eot



        [Fact]
        public async void SearchForSerialFieldShouldWork()
        {
            //CREATE A WIDGET
            dynamic D = new JObject();
            D.name = Util.Uniquify("Serial search test WIDGET");
            D.dollarAmount = 1.11m;
            D.active = true;
            D.roles = 0;
            D.notes = "Test for serial number search";

            ApiResponse a = await Util.PostAsync("Widget", await Util.GetTokenAsync("manager", "l3tm3in"), D.ToString());
            Util.ValidateDataReturnResponseOk(a);
            long MatchWidgetInSerialId = a.ObjectResponse["data"]["id"].Value<long>();
            var MatchWidgetSerial = a.ObjectResponse["data"]["serial"].Value<uint>();

            //TODO: get this from the return object
            string SerialSearch = MatchWidgetSerial.ToString(); ;


            //Now see if can find those objects with a phrase search
            dynamic SearchParameters = new JObject();
            SearchParameters.phrase = SerialSearch;
            SearchParameters.nameOnly = false;
            SearchParameters.typeOnly = 0;//no type
            SearchParameters.maxResults = 0;
            a = await Util.PostAsync("Search", await Util.GetTokenAsync("manager", "l3tm3in"), SearchParameters.ToString());
            Util.ValidateDataReturnResponseOk(a);

            //Now validate the return list
            ((JArray)a.ObjectResponse["data"]["searchResults"]).Count.Should().BeGreaterOrEqualTo(1);

            //Turn the list into an array of id's
            var v = ((JArray)a.ObjectResponse["data"]["searchResults"]);
            List<long> MatchingIdList = new List<long>();
            foreach (JObject j in v)
            {
                MatchingIdList.Add(j["id"].Value<long>());
            }

            //Ensure the expected items are returned
            MatchingIdList.Should().Contain(MatchWidgetInSerialId, "ShouldContainMatchWidgetInSerialId");


        }//eot


        //==================================================

    }//eoc
}//eons
