using IdokladSdk.Models.CashRegister;
using IdokladSdk.Models.CashVoucher;
using IdokladSdk.Models.ReceivedReceipt.Patch;
using IdokladSdk.Models.ReceivedReceipt.Post;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;

namespace Idoklad.Controllers;

/// <summary>
/// Received receipts and cash agendas. Requires iDoklad credentials (headers or environment defaults).
/// </summary>
[ApiController]
[Produces("application/json")]
[Tags("PurchaseAndCash")]
public sealed class PurchaseCashController : ControllerBase
{
    private readonly PurchaseCashService _service;

    public PurchaseCashController(PurchaseCashService service) => _service = service;

    // ---- Received receipts ----

    /// <summary>List received receipts (paged).</summary>
    [HttpGet("received-receipts")]
    public async Task<IActionResult> ListReceivedReceipts(int page = 1, int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListReceivedReceiptsAsync(page, pageSize, ct));

    /// <summary>Get a pre-filled default model for creating a new received receipt.</summary>
    [HttpGet("received-receipts/default")]
    public async Task<IActionResult> ReceivedReceiptDefault(CancellationToken ct)
        => Ok(await _service.ReceivedReceiptDefaultAsync(ct));

    /// <summary>Get a received receipt detail by id.</summary>
    [HttpGet("received-receipts/{id:int}")]
    public async Task<IActionResult> ReceivedReceiptDetail(int id, CancellationToken ct)
        => Ok(await _service.ReceivedReceiptDetailAsync(id, ct));

    /// <summary>Create a new received receipt.</summary>
    [HttpPost("received-receipts")]
    public async Task<IActionResult> CreateReceivedReceipt([FromBody] ReceivedReceiptPostModel model, CancellationToken ct)
        => Ok(await _service.CreateReceivedReceiptAsync(model, ct));

    /// <summary>Update an existing received receipt (the model id identifies the document).</summary>
    [HttpPatch("received-receipts")]
    public async Task<IActionResult> UpdateReceivedReceipt([FromBody] ReceivedReceiptPatchModel model, CancellationToken ct)
        => Ok(await _service.UpdateReceivedReceiptAsync(model, ct));

    /// <summary>Delete a received receipt by id.</summary>
    [HttpDelete("received-receipts/{id:int}")]
    public async Task<IActionResult> DeleteReceivedReceipt(int id, CancellationToken ct)
        => Ok(await _service.DeleteReceivedReceiptAsync(id, ct));

    // ---- Cash vouchers ----

    /// <summary>List cash vouchers (paged).</summary>
    [HttpGet("cash-vouchers")]
    public async Task<IActionResult> ListCashVouchers(int page = 1, int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListCashVouchersAsync(page, pageSize, ct));

    /// <summary>Get a cash voucher detail by id.</summary>
    [HttpGet("cash-vouchers/{id:int}")]
    public async Task<IActionResult> CashVoucherDetail(int id, CancellationToken ct)
        => Ok(await _service.CashVoucherDetailAsync(id, ct));

    /// <summary>Create a new cash voucher.</summary>
    [HttpPost("cash-vouchers")]
    public async Task<IActionResult> CreateCashVoucher([FromBody] CashVoucherPostModel model, CancellationToken ct)
        => Ok(await _service.CreateCashVoucherAsync(model, ct));

    /// <summary>Update an existing cash voucher (the model id identifies the document).</summary>
    [HttpPatch("cash-vouchers")]
    public async Task<IActionResult> UpdateCashVoucher([FromBody] CashVoucherPatchModel model, CancellationToken ct)
        => Ok(await _service.UpdateCashVoucherAsync(model, ct));

    /// <summary>Delete a cash voucher by id.</summary>
    [HttpDelete("cash-vouchers/{id:int}")]
    public async Task<IActionResult> DeleteCashVoucher(int id, CancellationToken ct)
        => Ok(await _service.DeleteCashVoucherAsync(id, ct));

    // ---- Cash registers ----

    /// <summary>List cash registers (paged).</summary>
    [HttpGet("cash-registers")]
    public async Task<IActionResult> ListCashRegisters(int page = 1, int pageSize = 20, CancellationToken ct = default)
        => Ok(await _service.ListCashRegistersAsync(page, pageSize, ct));

    /// <summary>Get a cash register detail by id.</summary>
    [HttpGet("cash-registers/{id:int}")]
    public async Task<IActionResult> CashRegisterDetail(int id, CancellationToken ct)
        => Ok(await _service.CashRegisterDetailAsync(id, ct));

    /// <summary>Create a new cash register.</summary>
    [HttpPost("cash-registers")]
    public async Task<IActionResult> CreateCashRegister([FromBody] CashRegisterPostModel model, CancellationToken ct)
        => Ok(await _service.CreateCashRegisterAsync(model, ct));

    /// <summary>Update an existing cash register (the model id identifies the register).</summary>
    [HttpPatch("cash-registers")]
    public async Task<IActionResult> UpdateCashRegister([FromBody] CashRegisterPatchModel model, CancellationToken ct)
        => Ok(await _service.UpdateCashRegisterAsync(model, ct));

    /// <summary>Delete a cash register by id.</summary>
    [HttpDelete("cash-registers/{id:int}")]
    public async Task<IActionResult> DeleteCashRegister(int id, CancellationToken ct)
        => Ok(await _service.DeleteCashRegisterAsync(id, ct));
}
