using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace AdsBillingService.API.Controllers;

/// <summary>
/// EN: API Controller for managing credit lines.
/// VI: API Controller quản lý hạn mức tín dụng.
/// </summary>
[ApiController]
[Route("api/v1/ads-billing/credit-lines")]
[Produces("application/json")]
public class CreditLinesController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<CreditLinesController> _logger;

    public CreditLinesController(IMediator mediator, ILogger<CreditLinesController> logger)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// EN: Get credit line information for an advertiser.
    /// VI: Lấy thông tin hạn mức tín dụng của advertiser.
    /// </summary>
    [HttpGet("{advertiserId}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetCreditLine(Guid advertiserId)
    {
        _logger.LogInformation("Getting credit line for advertiser {AdvertiserId}", advertiserId);

        // TODO: Implement GetCreditLineQuery
        // EN: For now return placeholder / VI: Tạm thời trả về placeholder
        return Ok(new
        {
            advertiserId,
            creditLimit = 0m,
            availableCredit = 0m,
            status = "Active",
            message = "Credit line query not yet implemented"
        });
    }

    /// <summary>
    /// EN: Request credit limit increase.
    /// VI: Yêu cầu tăng hạn mức tín dụng.
    /// </summary>
    [HttpPost("request")]
    [ProducesResponseType(StatusCodes.Status202Accepted)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> RequestCreditIncrease([FromBody] CreditIncreaseRequest request)
    {
        _logger.LogInformation("Credit increase request for advertiser {AdvertiserId}, amount {Amount}", 
            request.AdvertiserId, request.RequestedAmount);

        if (request.RequestedAmount <= 0)
        {
            return BadRequest(new { message = "Requested amount must be positive" });
        }

        // TODO: Implement RequestCreditIncreaseCommand
        // EN: For now return accepted / VI: Tạm thời trả về accepted
        return Accepted(new
        {
            advertiserId = request.AdvertiserId,
            requestedAmount = request.RequestedAmount,
            status = "Pending",
            message = "Credit increase request submitted for review"
        });
    }
}

/// <summary>
/// EN: Request model for credit increase.
/// VI: Request model cho yêu cầu tăng tín dụng.
/// </summary>
public record CreditIncreaseRequest(
    Guid AdvertiserId,
    decimal RequestedAmount,
    string? Reason = null
);
