using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Multitenant.TenantUser.Cqrs;

public record DeleteTenantUserByIdRequest(string Id);

public record DeleteTenantUserByIdCommand(DeleteTenantUserByIdRequest Data) : IRequest<bool>;

public class DeleteTenantUserByIdHandler : IRequestHandler<DeleteTenantUserByIdCommand, bool>
{
    private readonly AppDbContext _context;

    public DeleteTenantUserByIdHandler(AppDbContext context) => _context = context;

    public async Task<bool> Handle(DeleteTenantUserByIdCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.TenantUser
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

        if (entity == null) return false;

        _context.TenantUser.Remove(entity);
        return await _context.SaveChangesAsync(cancellationToken) > 0;
    }
}