using MediatR;
using SocialService.Domain.AggregatesModel.RelationshipAggregate;

namespace SocialService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetRelationshipByIdQuery.
/// VI: Handler cho GetRelationshipByIdQuery.
/// </summary>
public class GetRelationshipByIdQueryHandler : IRequestHandler<GetRelationshipByIdQuery, RelationshipDetailDto?>
{
    private readonly IRelationshipRepository _relationshipRepository;

    public GetRelationshipByIdQueryHandler(IRelationshipRepository relationshipRepository)
    {
        _relationshipRepository = relationshipRepository;
    }

    public async Task<RelationshipDetailDto?> Handle(GetRelationshipByIdQuery request, CancellationToken cancellationToken)
    {
        var relationship = await _relationshipRepository.GetByIdAsync(request.Id);
        
        if (relationship == null)
            return null;

        return new RelationshipDetailDto(
            relationship.Id,
            relationship.RequesterId,
            relationship.AddresseeId,
            relationship.Type.Name,
            relationship.Status.Name,
            relationship.CreatedAt,
            relationship.UpdatedAt);
    }
}
