// EN: Strategy for PreparedFood products (F&B).
// VI: Strategy cho sản phẩm PreparedFood (F&B).

using OrderService.Domain.AggregatesModel.OrderAggregate;
using OrderService.Domain.Strategies;
using OrderService.API.Infrastructure.HttpClients;

namespace OrderService.API.Application.Strategies;

/// <summary>
/// EN: Strategy for PreparedFood products - routes to F&amp;B Engine.
/// VI: Strategy cho sản phẩm PreparedFood - route đến F&amp;B Engine.
/// </summary>
public class FnbStrategy : ILineItemStrategy
{
    private readonly FnbEngineClient _fnbClient;
    private readonly ILogger<FnbStrategy> _logger;

    public string SupportedType => "PreparedFood";

    public FnbStrategy(
        FnbEngineClient fnbClient,
        ILogger<FnbStrategy> logger)
    {
        _fnbClient = fnbClient ?? throw new ArgumentNullException(nameof(fnbClient));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public Task<bool> ValidateAsync(
        OrderItem item,
        Guid shopId,
        CancellationToken cancellationToken = default)
    {
        _logger.LogInformation(
            "EN: Validating PreparedFood product / VI: Validate sản phẩm PreparedFood: {ProductName}",
            item.ProductName);

        // EN: F&B items are always valid (no inventory check)
        // VI: F&B items luôn hợp lệ (không cần kiểm tra kho)
        return Task.FromResult(true);
    }

    public async Task ExecuteAsync(
        OrderItem item,
        Guid shopId,
        CancellationToken cancellationToken = default)
    {
        _logger.LogInformation(
            "EN: Executing PreparedFood product / VI: Thực thi sản phẩm PreparedFood: {ProductName}",
            item.ProductName);

        // EN: Create kitchen ticket
        // VI: Tạo phiếu bếp
        var ticketId = await _fnbClient.CreateKitchenTicketAsync(
            item.ProductId,
            item.ProductName,
            shopId,
            item.Quantity,
            null, // Notes can be added later
            cancellationToken);

        if (ticketId == null)
        {
            throw new InvalidOperationException(
                $"EN: Failed to create kitchen ticket / VI: Tạo phiếu bếp thất bại: {item.ProductName}");
        }

        _logger.LogInformation(
            "EN: Kitchen ticket created / VI: Phiếu bếp đã tạo: {ProductName}, TicketId: {TicketId}",
            item.ProductName,
            ticketId);
    }
}
