using System.ComponentModel.DataAnnotations;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using isnd.Attributes;
using isnd.Entities;
using isn.abst;

namespace isnd.Controllers
{

    public partial class PackagesController
    {
        // Web get the paquet
        [HttpGet("~" + Constants.ApiVersion + ApiConfig.Package + "/{id}/{lower}/{idf}-{lowerf}."
        + Constants.PaquetFileEstension)]
        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}." + Constants.PaquetFileEstension
            );

            FileInfo pkgfi = new FileInfo(pkgpath);

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

        // Web get spec
        [HttpGet("~" + Constants.ApiVersion + Constants.SpecFileEstension + "/{id}/{lower}/{idf}-{lowerf}."
        + Constants.SpecFileEstension)]
        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}." + Constants.SpecFileEstension);

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

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