using Indotalent.Infrastructure.Database;
using MediatR;

namespace Indotalent.Features.Multitenant.TenantUser.Cqrs;

public class CreateTenantUserRequest
{
    public string? TenantId { get; set; }
    public string? UserId { get; set; }
    public string? Summary { get; set; }
    public bool IsActive { get; set; } = true;
}

public class CreateTenantUserResponse
{
    public string? Id { get; set; }
    public string? Summary { get; set; }
}

public record CreateTenantUserCommand(CreateTenantUserRequest Data) : IRequest<CreateTenantUserResponse>;

public class CreateTenantUserHandler : IRequestHandler<CreateTenantUserCommand, CreateTenantUserResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateTenantUserResponse> Handle(CreateTenantUserCommand request, CancellationToken cancellationToken)
    {
        var entity = new Data.Entities.TenantUser
        {
            TenantId = request.Data.TenantId,
            UserId = request.Data.UserId,
            Summary = request.Data.Summary,
            IsActive = request.Data.IsActive
        };

        _context.TenantUser.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);

        return new CreateTenantUserResponse
        {
            Id = entity.Id,
            Summary = entity.Summary
        };
    }
}