using IdokladSdk.Models.CashRegister;
using IdokladSdk.Models.CashVoucher;
using IdokladSdk.Models.ReceivedReceipt.Patch;
using IdokladSdk.Models.ReceivedReceipt.Post;
using Idoklad.Client;

namespace Idoklad.Services;

/// <summary>
/// Purchase-side receipts and cash agendas: received receipts, received documents (read-only),
/// cash vouchers and cash registers.
/// </summary>
public sealed class PurchaseCashService
{
    private readonly IdokladApiAccessor _accessor;

    public PurchaseCashService(IdokladApiAccessor accessor) => _accessor = accessor;

    // ---- Received receipts ----
    public Task<object?> ListReceivedReceiptsAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.ReceivedReceiptClient.List().ToPageAsync(page, pageSize, ct);

    public Task<object?> ReceivedReceiptDetailAsync(int id, CancellationToken ct)
        => _accessor.Api.ReceivedReceiptClient.Detail(id).ToDetailAsync(ct);

    public async Task<object?> ReceivedReceiptDefaultAsync(CancellationToken ct)
        => (await _accessor.Api.ReceivedReceiptClient.DefaultAsync(ct)).Unwrap();

    public async Task<object?> CreateReceivedReceiptAsync(ReceivedReceiptPostModel model, CancellationToken ct)
        => (await _accessor.Api.ReceivedReceiptClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdateReceivedReceiptAsync(ReceivedReceiptPatchModel model, CancellationToken ct)
        => (await _accessor.Api.ReceivedReceiptClient.UpdateAsync(model, ct)).Unwrap();

    public async Task<bool> DeleteReceivedReceiptAsync(int id, CancellationToken ct)
        => (await _accessor.Api.ReceivedReceiptClient.DeleteAsync(id, ct)).Unwrap();

    // ---- Cash vouchers ----
    public Task<object?> ListCashVouchersAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.CashVoucherClient.List().ToPageAsync(page, pageSize, ct);

    public Task<object?> CashVoucherDetailAsync(int id, CancellationToken ct)
        => _accessor.Api.CashVoucherClient.Detail(id).ToDetailAsync(ct);

    public async Task<object?> CreateCashVoucherAsync(CashVoucherPostModel model, CancellationToken ct)
        => (await _accessor.Api.CashVoucherClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdateCashVoucherAsync(CashVoucherPatchModel model, CancellationToken ct)
        => (await _accessor.Api.CashVoucherClient.UpdateAsync(model, ct)).Unwrap();

    public async Task<bool> DeleteCashVoucherAsync(int id, CancellationToken ct)
        => (await _accessor.Api.CashVoucherClient.DeleteAsync(id, ct)).Unwrap();

    // ---- Cash registers ----
    public Task<object?> ListCashRegistersAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.CashRegisterClient.List().ToPageAsync(page, pageSize, ct);

    public Task<object?> CashRegisterDetailAsync(int id, CancellationToken ct)
        => _accessor.Api.CashRegisterClient.Detail(id).ToDetailAsync(ct);

    public async Task<object?> CreateCashRegisterAsync(CashRegisterPostModel model, CancellationToken ct)
        => (await _accessor.Api.CashRegisterClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdateCashRegisterAsync(CashRegisterPatchModel model, CancellationToken ct)
        => (await _accessor.Api.CashRegisterClient.UpdateAsync(model, ct)).Unwrap();

    public async Task<bool> DeleteCashRegisterAsync(int id, CancellationToken ct)
        => (await _accessor.Api.CashRegisterClient.DeleteAsync(id, ct)).Unwrap();
}
