using MediatR;
using TakeoutSaaS.Application.App.Finance.Invoice.Dto;
using TakeoutSaaS.Application.App.Finance.Invoice.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Finance.Invoice.Handlers;

/// <summary>
/// 发票设置详情查询处理器。
/// </summary>
public sealed class GetFinanceInvoiceSettingDetailQueryHandler(
    ITenantInvoiceRepository repository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetFinanceInvoiceSettingDetailQuery, FinanceInvoiceSettingDto>
{
    /// <inheritdoc />
    public async Task<FinanceInvoiceSettingDto> Handle(
        GetFinanceInvoiceSettingDetailQuery request,
        CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var setting = await repository.GetSettingAsync(tenantId, cancellationToken);

        return setting is null
            ? FinanceInvoiceDtoFactory.CreateDefaultSettingDto()
            : FinanceInvoiceDtoFactory.ToSettingDto(setting);
    }
}
