using IdokladSdk.Models.BankAccount;
using IdokladSdk.Models.NumericSequence;
using IdokladSdk.Models.ReadOnly.VatRate;
using IdokladSdk.Response;
using Idoklad.Client;

namespace Idoklad.Services;

/// <summary>
/// Supporting registers: bank accounts, VAT rates and numeric (document) sequences.
/// </summary>
public sealed class RegistersService
{
    private readonly IdokladApiAccessor _accessor;

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

    // Bank accounts
    public async Task<Page<BankAccountListGetModel>> ListBankAccountsAsync(int page, int pageSize, CancellationToken ct)
        => (await _accessor.Api.BankAccountClient.List().Page(page).PageSize(pageSize).GetAsync(ct)).Unwrap();

    public async Task<BankAccountGetModel> BankAccountDetailAsync(int id, CancellationToken ct)
        => (await _accessor.Api.BankAccountClient.Detail(id).GetAsync(ct)).Unwrap();

    public async Task<BankAccountGetModel> CreateBankAccountAsync(BankAccountPostModel model, CancellationToken ct)
        => (await _accessor.Api.BankAccountClient.PostAsync(model, ct)).Unwrap();

    public async Task<BankAccountGetModel> UpdateBankAccountAsync(BankAccountPatchModel model, CancellationToken ct)
        => (await _accessor.Api.BankAccountClient.UpdateAsync(model, ct)).Unwrap();

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

    // VAT rates (read-only)
    public async Task<Page<VatRateListGetModel>> ListVatRatesAsync(int page, int pageSize, CancellationToken ct)
        => (await _accessor.Api.VatRateClient.List().Page(page).PageSize(pageSize).GetAsync(ct)).Unwrap();

    public async Task<VatRateGetModel> VatRateDetailAsync(int id, CancellationToken ct)
        => (await _accessor.Api.VatRateClient.Detail(id).GetAsync(ct)).Unwrap();

    // Numeric sequences (read-only list)
    public async Task<Page<NumericSequenceGetModel>> ListNumericSequencesAsync(int page, int pageSize, CancellationToken ct)
        => (await _accessor.Api.NumericSequenceClient.List().Page(page).PageSize(pageSize).GetAsync(ct)).Unwrap();
}
