
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using rockfishCore.Models;
using rockfishCore.Util;
using System.Linq;
using System;

//requried to inject configuration in constructor
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;

using Microsoft.EntityFrameworkCore;

namespace rockfishCore.Controllers
{
    [Produces("application/json")]
    [Route("api/autocomplete")]
    [Authorize]
    public class AutocompleteController : Controller
    {
        private readonly rockfishContext _context;
        private readonly IConfiguration _configuration;
        public AutocompleteController(rockfishContext context, IConfiguration configuration)//these two are injected, see startup.cs
        {
            _context = context;
            _configuration = configuration;
        }



       
        [HttpGet]
        public JsonResult GetAutocomplete(string acget, string query) //get parameters from url not body
        {
             //TODO: if, in future need more might want to make acget a csv string for multi table / field search
            //ACGET is a collection name and field 'purchase.name'
            //QUERY is just the characters typed so far, could be one or more

            //default return value, emtpy suggestions
            var ret = new { suggestions = new string[] { } };

            if (string.IsNullOrWhiteSpace(acget) || string.IsNullOrWhiteSpace(query))
            {
                return Json(ret);
            }


            string[] acgetsplit = acget.Split('.');
            string col = acgetsplit[0];
            string fld = acgetsplit[1];

            List<string> resultList = new List<string>();

            using (var command = _context.Database.GetDbConnection().CreateCommand())
            {

                command.CommandText = "SELECT DISTINCT " + fld + " From " + col + " where " + fld + " like @q ORDER BY " + fld + ";";
                var parameter = command.CreateParameter();
                parameter.ParameterName = "@q";
                parameter.Value = "%" + query + "%";
                command.Parameters.Add(parameter);

                _context.Database.OpenConnection();
                using (var res = command.ExecuteReader())
                {

                    if (res.HasRows)
                    {
                        while (res.Read())
                        {
                            resultList.Add(res.GetString(0));
                        }
                    }
                }
                _context.Database.CloseConnection();
            }
            return Json(new { suggestions = resultList });
        }




        //------------------------------------------------------

    }//eoc
}//eons


//enter 'a' in purchase product name get:

/*
GET request url:
https://rockfish.ayanova.com/api/autocomplete?acget=purchase.name&query=A

Response:
{"suggestions":["CANCELED Up to 5","CANCELED RI","CANCELED WBI","QBI Renewal","OLI Renewal","MBI Renewal","CANCELED Outlook Schedule Export","Quick Notification Renewal","RI Renewal","WBI Renewal","Up to 5 Renewal","Outlook Schedule Export Renewal","Single Renewal","Export to  XLS Renewal","Importexport.csv duplicate Renewal","Export To XLS Renewal","Up to 10 Renewal","Up to 20 RENEWAL","CANCELED Single","Quick Notification","Importexport.csv duplicate","CANCELED MBI","CANCELED OLI","CANCELED QBI","CANCELED Quick Notification","Quick Notification ","Key Administration","AyaNova Lite","PTI - Renewal"]} */
