using Microsoft.AspNetCore.Mvc;
using WebClientTpos.Server.Infrastructure;

namespace WebClientTpos.Server.Controllers;

/// <summary>
/// EN: Reports controller — proxies to OrderService for revenue reports and top products.
/// VI: Controller báo cáo — proxy đến OrderService cho báo cáo doanh thu và sản phẩm bán chạy.
/// </summary>
[ApiController]
[Route("api/bff")]
public class ReportsController : ControllerBase
{
    private readonly HttpClient _order;

    public ReportsController(IHttpClientFactory httpClientFactory)
    {
        _order = httpClientFactory.CreateClient("OrderService");
    }

    /// <summary>
    /// EN: Get revenue report grouped by day/week/month.
    /// VI: Lấy báo cáo doanh thu theo ngày/tuần/tháng.
    /// </summary>
    [HttpGet("reports/revenue")]
    public Task<IActionResult> GetRevenueReport(
        [FromQuery] string period = "daily",
        [FromQuery] Guid? shopId = null)
    {
        var qs = new List<string> { $"period={Uri.EscapeDataString(period)}" };
        if (shopId.HasValue) qs.Add($"shopId={shopId}");
        return _order.GetAsync($"/api/v1/reports/revenue?{string.Join("&", qs)}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get top-selling products.
    /// VI: Lấy sản phẩm bán chạy nhất.
    /// </summary>
    [HttpGet("reports/top-products")]
    public Task<IActionResult> GetTopProducts(
        [FromQuery] Guid? shopId = null,
        [FromQuery] int limit = 10)
    {
        var qs = new List<string> { $"limit={limit}" };
        if (shopId.HasValue) qs.Add($"shopId={shopId}");
        return _order.GetAsync($"/api/v1/reports/top-products?{string.Join("&", qs)}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get advanced revenue analytics with trends, payment methods, and vertical breakdown.
    /// VI: Lay phan tich doanh thu nang cao voi xu huong, phuong thuc thanh toan, va phan tich theo nganh.
    /// </summary>
    [HttpGet("reports/revenue-analytics")]
    public Task<IActionResult> GetRevenueAnalytics(
        [FromQuery] Guid shopId,
        [FromQuery] string startDate,
        [FromQuery] string endDate,
        [FromQuery] string period = "daily")
    {
        var qs = new List<string>
        {
            $"shopId={shopId}",
            $"startDate={Uri.EscapeDataString(startDate)}",
            $"endDate={Uri.EscapeDataString(endDate)}",
            $"period={Uri.EscapeDataString(period)}"
        };
        return _order.GetAsync($"/api/v1/reports/revenue-analytics?{string.Join("&", qs)}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get staff performance metrics for a shop.
    /// VI: Lay chi so hieu suat nhan vien cho shop.
    /// </summary>
    [HttpGet("reports/staff-performance")]
    public Task<IActionResult> GetStaffPerformance(
        [FromQuery] Guid shopId,
        [FromQuery] string startDate,
        [FromQuery] string endDate)
    {
        var qs = new List<string>
        {
            $"shopId={shopId}",
            $"startDate={Uri.EscapeDataString(startDate)}",
            $"endDate={Uri.EscapeDataString(endDate)}"
        };
        return _order.GetAsync($"/api/v1/reports/staff-performance?{string.Join("&", qs)}").ProxyAsync();
    }

    /// <summary>
    /// EN: Get End-of-Day report for a shop.
    /// VI: Lay bao cao cuoi ngay cho shop.
    /// </summary>
    [HttpGet("reports/eod")]
    public Task<IActionResult> GetEodReport(
        [FromQuery] Guid? shopId = null,
        [FromQuery] string? date = null)
    {
        var qs = new List<string>();
        if (shopId.HasValue) qs.Add($"shopId={shopId}");
        if (!string.IsNullOrEmpty(date)) qs.Add($"date={Uri.EscapeDataString(date)}");
        var query = qs.Any() ? "?" + string.Join("&", qs) : "";
        return _order.GetAsync($"/api/v1/reports/eod{query}").ProxyAsync();
    }

    /// <summary>
    /// EN: Close the business day and generate final EOD report.
    /// VI: Dong ngay kinh doanh va tao bao cao cuoi ngay.
    /// </summary>
    [HttpPost("reports/close-day")]
    public Task<IActionResult> CloseDay([FromBody] CloseDayBffRequest request)
    {
        return _order.PostAsJsonAsync("/api/v1/reports/close-day", new
        {
            shopId = request.ShopId,
            closeDate = request.CloseDate
        }).ProxyAsync();
    }
}

/// <summary>
/// EN: BFF request for closing the business day.
/// VI: BFF request de dong ngay kinh doanh.
/// </summary>
public record CloseDayBffRequest(Guid ShopId, string? CloseDate = null);
