using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text;
//using System.Web.Mvc;

public class DownloadController : Controller
{
    [HttpGet]
    [Route("download/row-json/{id}")]
    public async Task<ActionResult> RowJson(string id)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            return BadRequest();
        }

        using (var httpClient = new HttpClient())
        {
            try
            {
                var response = await httpClient.GetAsync($"https://demodata.grapecity.com/northwind/api/v1/Products/{id}");

                if (!response.IsSuccessStatusCode)
                {
                    return NotFound($"Product with ID {id} not found.");
                }

                var jsonContent = await response.Content.ReadAsStringAsync();
                byte[] fileBytes = Encoding.UTF8.GetBytes(jsonContent);
                string fileName = $"product_{id}.json";

                return File(fileBytes, "application/json", fileName);
            }
            catch (HttpRequestException ex)
            {
                return StatusCode(500, $"Error fetching data: {ex.Message}");
            }
        }
    }
}
