using System.Linq;

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using isnd.Entities;
using isn.abst;
using isnd.Data.Catalog;

namespace isnd.Controllers
{

    public partial class PackagesController
    {
        // GET {@id}?q={QUERY}&skip={SKIP}&take={TAKE}&prerelease={PRERELEASE}&semVerLevel={SEMVERLEVEL}&packageType={PACKAGETYPE}
        [HttpGet("~" + Constants.ApiVersionPrefix + ApiConfig.Find)]
        [HttpPost("~" + Constants.ApiVersionPrefix + ApiConfig.Find)]
        public async Task<IActionResult> Search(
            string id,
            bool prerelease = false,
           // string packageType = null,
            string semVerLevel = null,
            int skip = 0,
            int take = 25
        )
        {
            id = id.Trim('\'');
            if (semVerLevel == "2.0.0") prerelease = true;

            //packageManager.SearchCatalogEntriesById(id, semVerLevel, packageType, prerelease);
            var regs = await packageManager.SearchPackageAsync(

                new PackageRegistrationQuery
                {
                    Query = id,
                    Prerelease = prerelease,
                    Take = take,
                    Skip = skip
                }
            );
            return Ok(new { totalHits = regs.Count(), data = regs });
        }
    }
}