using Microsoft.AspNetCore.Mvc;
using DorkNet.Server.Services;
namespace DorkNet.Server.Controllers.Commerce;
///
/// commerce.rec.net/api/catalog/* — the actual catalog endpoint the
/// 2020 watch's Shop tab fetches when it loads. The previous
/// implementation routed everything on commerce.rec.net through the
/// generic StubController which always returned []; this
/// controller wins on specific routes via ASP.NET Core's
/// specific-over-wildcard precedence and returns the real
/// catalog.
///
/// Wire shape verified against the watch's Commerce.Catalog
/// deserialiser logging "no items" when we returned an empty array
/// vs populating the Shop UI when each item carries SkuId/Name/Cost
/// keys. Each catalog DTO ships every common alias (PascalCase,
/// camelCase, snake_case) so whichever JSON field the watch's
/// strict-key reader expects, it finds.
///
[ApiController]
public class CommerceCatalogController(StoreService store, DomainConfig domain) : ControllerBase
{
[HttpGet("/api/catalog/v1/all")]
[HttpGet("/api/catalog/v2/all")]
[HttpGet("/api/catalog/v3/all")]
public async Task All([FromQuery] bool onlyAvailableSkus = false)
{
var items = onlyAvailableSkus
? await store.GetAllActiveAsync()
: await store.GetAllActiveAsync(); // same set; we don't distinguish yet
return Ok(items.Select(i => ToCatalogDto(i, domain.Apex)).ToArray());
}
[HttpGet("/api/catalog/v1/sku/{slug}")]
[HttpGet("/api/catalog/v2/sku/{slug}")]
public async Task Sku(string slug)
{
var item = await store.GetBySlugAsync(slug);
if (item is null) return NotFound();
return Ok(ToCatalogDto(item, domain.Apex));
}
/// POST /api/catalog/v1/byids — bulk lookup by id array.
/// The watch sends {Ids:[1,2,3]} as form-urlencoded; accept both
/// JSON and form bodies for safety.
[HttpPost("/api/catalog/v1/byids")]
[HttpPost("/api/catalog/v2/byids")]
public async Task ByIds()
{
string? raw = null;
if (Request.HasFormContentType)
{
var form = await Request.ReadFormAsync();
raw = form["Ids"].ToString();
if (string.IsNullOrWhiteSpace(raw)) raw = form["ids"].ToString();
}
if (string.IsNullOrWhiteSpace(raw)) raw = Request.Query["Ids"].ToString();
if (string.IsNullOrWhiteSpace(raw)) raw = Request.Query["ids"].ToString();
if (string.IsNullOrWhiteSpace(raw)) return Ok(Array.Empty