using IdokladSdk.Models.BankAccount;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;

namespace Idoklad.Controllers;

/// <summary>
/// Supporting registers: bank accounts, VAT rates and numeric sequences. Requires iDoklad
/// credentials (headers or environment defaults).
/// </summary>
[ApiController]
[Route("registers")]
[Produces("application/json")]
[Tags("Registers")]
public sealed class RegistersController : ControllerBase
{
    private readonly RegistersService _service;

    public RegistersController(RegistersService service) => _service = service;

    // ---- Bank accounts ----

    /// <summary>List bank accounts (paged).</summary>
    [HttpGet("bank-accounts")]
    public async Task<IActionResult> ListBankAccounts([FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListBankAccountsAsync(page, pageSize, ct));

    /// <summary>Get a bank account detail by id.</summary>
    [HttpGet("bank-accounts/{id:int}")]
    public async Task<IActionResult> BankAccountDetail(int id, CancellationToken ct)
        => Ok(await _service.BankAccountDetailAsync(id, ct));

    /// <summary>Create a new bank account.</summary>
    [HttpPost("bank-accounts")]
    public async Task<IActionResult> CreateBankAccount([FromBody] BankAccountPostModel model, CancellationToken ct)
        => Ok(await _service.CreateBankAccountAsync(model, ct));

    /// <summary>Update an existing bank account (the model id identifies the account).</summary>
    [HttpPatch("bank-accounts")]
    public async Task<IActionResult> UpdateBankAccount([FromBody] BankAccountPatchModel model, CancellationToken ct)
        => Ok(await _service.UpdateBankAccountAsync(model, ct));

    /// <summary>Delete a bank account by id.</summary>
    [HttpDelete("bank-accounts/{id:int}")]
    public async Task<IActionResult> DeleteBankAccount(int id, CancellationToken ct)
        => Ok(await _service.DeleteBankAccountAsync(id, ct));

    // ---- VAT rates (read-only) ----

    /// <summary>List VAT rates (paged).</summary>
    [HttpGet("vat-rates")]
    public async Task<IActionResult> ListVatRates([FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListVatRatesAsync(page, pageSize, ct));

    /// <summary>Get a VAT rate detail by id.</summary>
    [HttpGet("vat-rates/{id:int}")]
    public async Task<IActionResult> VatRateDetail(int id, CancellationToken ct)
        => Ok(await _service.VatRateDetailAsync(id, ct));

    // ---- Numeric sequences (read-only) ----

    /// <summary>List numeric (document) sequences (paged).</summary>
    [HttpGet("numeric-sequences")]
    public async Task<IActionResult> ListNumericSequences([FromQuery] int page = 1, [FromQuery] int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListNumericSequencesAsync(page, pageSize, ct));
}
