using Femto.Common.Domain;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Femto.Modules.Media.Application;

internal class MediaModule(IHost host) : IMediaModule
{
    public async Task<TResult> PostCommand<TResult>(ICommand<TResult> command, CancellationToken cancellationToken)
    {
        using var scope = host.Services.CreateScope();
        var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
        var response = await mediator.Send(command, cancellationToken);
        return response;
    }

    public async Task<TResult> PostQuery<TResult>(IQuery<TResult> query, CancellationToken cancellationToken)
    {
        using var scope = host.Services.CreateScope();
        var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
        var response = await mediator.Send(query, cancellationToken);
        return response;
    }
}