using IdokladSdk.Models.PriceListItem;
using IdokladSdk.Models.StockMovement;
using IdokladSdk.Models.Tag;
using Idoklad.Client;

namespace Idoklad.Services;

/// <summary>
/// Catalog and stock agendas: price list items, stock movements and tags.
/// </summary>
public sealed class CatalogService
{
    private readonly IdokladApiAccessor _accessor;

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

    // ---- Price list items ----
    public Task<object?> ListPriceListItemsAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.PriceListItemClient.List().ToPageAsync(page, pageSize, ct);

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

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

    public async Task<object?> CreatePriceListItemAsync(PriceListItemPostModel model, CancellationToken ct)
        => (await _accessor.Api.PriceListItemClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdatePriceListItemAsync(PriceListItemPatchModel model, CancellationToken ct)
        => (await _accessor.Api.PriceListItemClient.UpdateAsync(model, ct)).Unwrap();

    // ---- Stock movements ----
    public Task<object?> ListStockMovementsAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.StockMovementClient.List().ToPageAsync(page, pageSize, ct);

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

    public async Task<object?> StockMovementDefaultAsync(int priceListItemId, CancellationToken ct)
        => (await _accessor.Api.StockMovementClient.DefaultAsync(priceListItemId, ct)).Unwrap();

    public async Task<object?> CreateStockMovementAsync(StockMovementPostModel model, CancellationToken ct)
        => (await _accessor.Api.StockMovementClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdateStockMovementAsync(StockMovementPatchModel model, CancellationToken ct)
        => (await _accessor.Api.StockMovementClient.UpdateAsync(model, ct)).Unwrap();

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

    // ---- Tags ----
    public Task<object?> ListTagsAsync(int page, int pageSize, CancellationToken ct)
        => _accessor.Api.TagClient.List().ToPageAsync(page, pageSize, ct);

    public async Task<object?> CreateTagAsync(TagPostModel model, CancellationToken ct)
        => (await _accessor.Api.TagClient.PostAsync(model, ct)).Unwrap();

    public async Task<object?> UpdateTagAsync(TagPatchModel model, CancellationToken ct)
        => (await _accessor.Api.TagClient.UpdateAsync(model, ct)).Unwrap();

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