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

namespace WebClientTpos.Server.Controllers;

/// <summary>
/// EN: Cafe controller — proxies stamp card endpoints to MembershipService
///     and barista queue endpoints to FnbEngine.
/// VI: Controller Cafe — proxy endpoint the tem den MembershipService
///     va endpoint hang doi barista den FnbEngine.
/// </summary>
[ApiController]
[Route("api/bff/cafe")]
public class CafeController : ControllerBase
{
    private readonly HttpClient _membership;
    private readonly HttpClient _fnb;

    public CafeController(IHttpClientFactory httpClientFactory)
    {
        _membership = httpClientFactory.CreateClient("MembershipService");
        _fnb = httpClientFactory.CreateClient("FnbEngine");
    }

    // ═══ STAMP CARDS (membership-service) ═══

    /// <summary>
    /// EN: Get a customer's stamp card for a specific shop.
    /// VI: Lay the tem cua khach hang cho mot cua hang cu the.
    /// </summary>
    [HttpGet("{shopId:guid}/stamp-cards/{memberId:guid}")]
    public Task<IActionResult> GetStampCard(Guid shopId, Guid memberId) =>
        _membership.GetAsync($"/api/v1/stamp-cards?shopId={shopId}&memberId={memberId}").ProxyAsync();

    /// <summary>
    /// EN: Add a stamp to the customer's card.
    /// VI: Them tem vao the khach hang.
    /// </summary>
    [HttpPost("{shopId:guid}/stamp-cards/{memberId:guid}/stamps")]
    public Task<IActionResult> AddStamp(Guid shopId, Guid memberId, [FromBody] JsonElement body) =>
        _membership.PostAsJsonAsync($"/api/v1/stamp-cards/{shopId}/{memberId}/stamps", body).ProxyAsync();

    /// <summary>
    /// EN: Claim reward for a completed stamp card.
    /// VI: Nhan thuong cho the tem da hoan tat.
    /// </summary>
    [HttpPost("stamp-cards/{stampCardId:guid}/claim")]
    public Task<IActionResult> ClaimReward(Guid stampCardId, [FromBody] JsonElement body) =>
        _membership.PostAsJsonAsync($"/api/v1/stamp-cards/{stampCardId}/claim", body).ProxyAsync();

    // ═══ BARISTA QUEUE (fnb-engine) ═══

    /// <summary>
    /// EN: Get all items in the barista queue for a shop.
    /// VI: Lay tat ca mon trong hang doi barista cua cua hang.
    /// </summary>
    [HttpGet("{shopId:guid}/barista-queue")]
    public Task<IActionResult> GetBaristaQueue(Guid shopId) =>
        _fnb.GetAsync($"/api/v1/barista-queue?shopId={shopId}").ProxyAsync();

    /// <summary>
    /// EN: Get aggregate stats for the barista queue.
    /// VI: Lay thong ke tong hop cho hang doi barista.
    /// </summary>
    [HttpGet("{shopId:guid}/barista-queue/stats")]
    public Task<IActionResult> GetBaristaQueueStats(Guid shopId) =>
        _fnb.GetAsync($"/api/v1/barista-queue/stats?shopId={shopId}").ProxyAsync();

    /// <summary>
    /// EN: Add a drink to the barista queue.
    /// VI: Them ly vao hang doi barista.
    /// </summary>
    [HttpPost("{shopId:guid}/barista-queue")]
    public Task<IActionResult> QueueDrink(Guid shopId, [FromBody] JsonElement body) =>
        _fnb.PostAsJsonAsync($"/api/v1/barista-queue?shopId={shopId}", body).ProxyAsync();

    /// <summary>
    /// EN: Start preparing a drink — assigns barista name.
    /// VI: Bat dau pha che — gan ten barista.
    /// </summary>
    [HttpPost("barista-queue/{queueItemId:guid}/start")]
    public Task<IActionResult> StartPreparing(Guid queueItemId, [FromBody] JsonElement body) =>
        _fnb.PostAsJsonAsync($"/api/v1/barista-queue/{queueItemId}/start", body).ProxyAsync();

    /// <summary>
    /// EN: Mark a drink as ready for pickup.
    /// VI: Danh dau ly da san sang de lay.
    /// </summary>
    [HttpPost("barista-queue/{queueItemId:guid}/ready")]
    public Task<IActionResult> MarkReady(Guid queueItemId, [FromBody] JsonElement body) =>
        _fnb.PostAsJsonAsync($"/api/v1/barista-queue/{queueItemId}/ready", body).ProxyAsync();

    /// <summary>
    /// EN: Mark a drink as delivered to the customer.
    /// VI: Danh dau ly da giao cho khach hang.
    /// </summary>
    [HttpPost("barista-queue/{queueItemId:guid}/delivered")]
    public Task<IActionResult> MarkDelivered(Guid queueItemId, [FromBody] JsonElement body) =>
        _fnb.PostAsJsonAsync($"/api/v1/barista-queue/{queueItemId}/delivered", body).ProxyAsync();
}
