using Microsoft.AspNetCore.Mvc;
using Csob.Services;

namespace Csob.Controllers;

/// <summary>
/// AISP – account information. All endpoints require the ČSOB credential headers
/// (eIDAS certificate, access token, API key, TPP name). See the Swagger description for details.
/// </summary>
[ApiController]
[Route("accounts")]
[Produces("application/json")]
[Tags("AISP – Accounts")]
public sealed class AccountsController : ControllerBase
{
    private readonly AccountsService _service;

    public AccountsController(AccountsService service) => _service = service;

    /// <summary>List the PSU's payment accounts (paged).</summary>
    [HttpGet]
    public async Task<IActionResult> List(
        [FromQuery] int? page, [FromQuery] int? size, [FromQuery] string? sort, [FromQuery] string? order, CancellationToken ct)
        => Ok(await _service.ListAsync(page, size, sort, order, ct));

    /// <summary>Get the balance(s) of an account.</summary>
    [HttpGet("{id}/balance")]
    public async Task<IActionResult> Balance(string id, [FromQuery] string? currency, CancellationToken ct)
        => Ok(await _service.BalanceAsync(id, currency, ct));

    /// <summary>List booked transactions of an account (paged, optional date range).</summary>
    [HttpGet("{id}/transactions")]
    public async Task<IActionResult> Transactions(
        string id,
        [FromQuery] int? page, [FromQuery] int? size, [FromQuery] string? sort, [FromQuery] string? order,
        [FromQuery] string? dateFrom, [FromQuery] string? dateTo, CancellationToken ct)
        => Ok(await _service.TransactionsAsync(id, new TransactionQuery
        {
            Page = page, Size = size, Sort = sort, Order = order, DateFrom = dateFrom, DateTo = dateTo,
        }, ct));

    /// <summary>List awaiting (pending) transactions of an account.</summary>
    [HttpGet("{id}/transactions/awaiting")]
    public async Task<IActionResult> Awaiting(string id, [FromQuery] int? page, [FromQuery] int? size, CancellationToken ct)
        => Ok(await _service.AwaitingTransactionsAsync(id, page, size, ct));

    /// <summary>List the account's existing standing orders.</summary>
    [HttpGet("{id}/standing-orders")]
    public async Task<IActionResult> StandingOrders(string id, [FromQuery] int? page, [FromQuery] int? size, CancellationToken ct)
        => Ok(await _service.StandingOrdersAsync(id, page, size, ct));

    /// <summary>Get a standing-order detail for the account.</summary>
    [HttpGet("{id}/standing-orders/{standingOrderId}")]
    public async Task<IActionResult> StandingOrderDetail(string id, string standingOrderId, CancellationToken ct)
        => Ok(await _service.StandingOrderDetailAsync(id, standingOrderId, ct));

    /// <summary>List the account's direct-debit mandates.</summary>
    [HttpGet("{id}/direct-debits")]
    public async Task<IActionResult> DirectDebits(string id, [FromQuery] int? page, [FromQuery] int? size, CancellationToken ct)
        => Ok(await _service.DirectDebitsAsync(id, page, size, ct));
}
