using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using WebClientTpos.Server.Infrastructure;

namespace WebClientTpos.Server.Controllers;

/// <summary>
/// EN: Catalog controller — proxies to CatalogService for product/category CRUD.
/// VI: Controller danh mục — proxy đến CatalogService cho CRUD sản phẩm/danh mục.
/// </summary>
[ApiController]
[Route("api/bff")]
public class CatalogController : ControllerBase
{
    private readonly HttpClient _catalog;

    public CatalogController(IHttpClientFactory httpClientFactory)
    {
        _catalog = httpClientFactory.CreateClient("CatalogService");
    }

    /// <summary>
    /// EN: Get products belonging to the current merchant's shops.
    /// VI: Lấy sản phẩm thuộc các cửa hàng của merchant hiện tại.
    /// </summary>
    [HttpGet("products")]
    public Task<IActionResult> GetAllProducts([FromQuery] Guid? shopId = null, [FromQuery] bool? isActive = true)
    {
        var qsList = new List<string>();
        if (shopId.HasValue) qsList.Add($"shopId={shopId}");
        if (isActive.HasValue) qsList.Add($"isActive={isActive.Value.ToString().ToLower()}");
        var qs = qsList.Any() ? "?" + string.Join("&", qsList) : "";
        return _catalog.GetAsync($"/api/v1/products{qs}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get products for a specific shop. POS-facing — only returns active products by default.
    /// VI: Lấy sản phẩm của một cửa hàng cụ thể. Dành cho POS — chỉ trả về sản phẩm đang hoạt động.
    /// </summary>
    [HttpGet("shops/{shopId}/products")]
    public Task<IActionResult> GetShopProducts(Guid shopId, [FromQuery] bool? isActive = true)
    {
        var qs = isActive.HasValue ? $"?isActive={isActive.Value.ToString().ToLower()}" : "";
        return _catalog.GetAsync($"/api/v1/shops/{shopId}/products{qs}").ProxyAsync();
    }

    /// <summary>
    /// EN: Create a product.
    /// VI: Tạo sản phẩm.
    /// </summary>
    [HttpPost("products")]
    public Task<IActionResult> CreateProduct([FromBody] JsonElement body) =>
        _catalog.PostAsJsonAsync("/api/v1/products", body).ProxyAsync();

    /// <summary>
    /// EN: Update a product.
    /// VI: Cập nhật sản phẩm.
    /// </summary>
    [HttpPut("products/{productId:guid}")]
    public Task<IActionResult> UpdateProduct(Guid productId, [FromBody] JsonElement body) =>
        _catalog.PutAsJsonAsync($"/api/v1/products/{productId}", body).ProxyAsync();

    /// <summary>
    /// EN: Delete (deactivate) a product.
    /// VI: Xóa (vô hiệu hóa) sản phẩm.
    /// </summary>
    [HttpDelete("products/{productId:guid}")]
    public Task<IActionResult> DeleteProduct(Guid productId) =>
        _catalog.DeleteAsync($"/api/v1/products/{productId}").ProxyAsync();

    /// <summary>
    /// EN: Get categories belonging to the current merchant's shops.
    /// VI: Lấy danh mục thuộc các cửa hàng của merchant hiện tại.
    /// </summary>
    [HttpGet("categories")]
    public Task<IActionResult> GetAllCategories([FromQuery] Guid? shopId = null)
    {
        var qs = shopId.HasValue ? $"?shopId={shopId}" : "";
        return _catalog.GetAsync($"/api/v1/categories{qs}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get categories for a specific shop.
    /// VI: Lấy danh mục của một cửa hàng cụ thể.
    /// </summary>
    [HttpGet("shops/{shopId}/categories")]
    public Task<IActionResult> GetShopCategories(Guid shopId) =>
        _catalog.GetAsync($"/api/v1/shops/{shopId}/categories").ProxyAsync();

    /// <summary>
    /// EN: Create a category.
    /// VI: Tạo danh mục.
    /// </summary>
    [HttpPost("categories")]
    public Task<IActionResult> CreateCategory([FromBody] JsonElement body) =>
        _catalog.PostAsJsonAsync("/api/v1/categories", body).ProxyAsync();

    /// <summary>
    /// EN: Update a category.
    /// VI: Cập nhật danh mục.
    /// </summary>
    [HttpPut("categories/{categoryId:guid}")]
    public Task<IActionResult> UpdateCategory(Guid categoryId, [FromBody] JsonElement body) =>
        _catalog.PutAsJsonAsync($"/api/v1/categories/{categoryId}", body).ProxyAsync();

    /// <summary>
    /// EN: Soft-delete a category.
    /// VI: Xóa mềm danh mục.
    /// </summary>
    [HttpDelete("categories/{categoryId:guid}")]
    public Task<IActionResult> DeleteCategory(Guid categoryId) =>
        _catalog.DeleteAsync($"/api/v1/categories/{categoryId}").ProxyAsync();
}
