using System.ComponentModel.DataAnnotations;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using isnd.Attributes;
namespace isnd.Controllers
{

    public partial class PackagesController
    {
        // TODO GET GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.{LOWER_VERSION}.nupkg
        // LOWER_ID 	URL 	string 	yes 	The package ID, lowercase
        // LOWER_VERSION 	URL 	string 	yes 	The package version, normalized and lowercased
        // response 200 : the package
        [HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nupkg")]
        public IActionResult GetPackage(
           [FromRoute][SafeName][Required] string id, 
           [FromRoute][SafeName][Required] string lower,
            [FromRoute] string idf, [FromRoute] string lowerf)
        {
            var pkgpath = Path.Combine(_isndSettings.PackagesRootDir,
            id, lower, $"{id}-{lower}.nupkg"
            );

            FileInfo pkgfi = new FileInfo(pkgpath);

            if (!pkgfi.Exists)
            {
                return BadRequest("!pkgfi.Exists");
            }
            return File(pkgfi.OpenRead(), "application/zip; charset=binary");
        }

        // TODO GET {@id}/{LOWER_ID}/{LOWER_VERSION}/{LOWER_ID}.nuspec
        // response 200 : the nuspec
        [HttpGet(_pkgRootPrefix + ApiConfig.Get + "/{id}/{lower}/{idf}-{lowerf}.nuspec")]
        public IActionResult GetNuspec(
            [FromRoute][SafeName][Required] string id,
            [FromRoute][SafeName][Required] string lower,
            [FromRoute][SafeName][Required] string idf,
            [FromRoute][SafeName][Required] string lowerf)
        {
            var pkgpath = Path.Combine(_isndSettings.PackagesRootDir,
            id, lower, $"{id}.nuspec");

            FileInfo pkgfi = new FileInfo(pkgpath);
            if (!pkgfi.Exists)
            {
                return BadRequest("!pkgfi.Exists");
            }

            return File(pkgfi.OpenRead(), "text/xml; charset=utf-8");
        }
    }
}