using Microsoft.AspNetCore.Mvc;
using Meezi.Core.Interfaces;
using Meezi.Shared;

namespace Meezi.API.Controllers;

[Route("api/cafes/{cafeId}/tax/taraz")]
public class TarazController : CafeApiControllerBase
{
    private readonly ITarazTaxService _taraz;

    public TarazController(ITarazTaxService taraz) => _taraz = taraz;

    [HttpPost("submit")]
    public async Task<IActionResult> Submit(
        string cafeId,
        ITenantContext tenant,
        [FromQuery] DateTime? date,
        CancellationToken ct)
    {
        if (EnsureCafeAccess(cafeId, tenant) is { } denied) return denied;

        var targetDate = date ?? DateTime.UtcNow.Date;
        var result = await _taraz.SubmitDailyInvoicesAsync(cafeId, targetDate, ct);
        if (!result.Success)
            return BadRequest(new ApiResponse<object>(false, null, new ApiError("TARAZ_ERROR", result.Message ?? "Submit failed.")));

        return Ok(new ApiResponse<object>(true, new { trackingCode = result.TrackingCode, message = result.Message }));
    }
}
