using System;
using System.Threading;
using System.Threading.Tasks;
using Knot.Contracts.Profiles.Domain;
using Knot.Contracts.Relations.Domain;
using Knot.Modules.Stories.Application.Abstractions;

namespace Knot.Modules.Stories.Infrastructure.Database;

internal sealed class UserRepository : IUserRepository
{
    private readonly IProfileRepository _profileRepository;

    public UserRepository(IProfileRepository profileRepository)
    {
        _profileRepository = profileRepository;
    }

    public async Task<bool> ExistsAsync(Guid id, CancellationToken cancellationToken = default)
    {
        var profile = await _profileRepository.GetAsync(id, cancellationToken);
        return profile != null;
    }

    public async Task<UserReplica?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
    {
        var profile = await _profileRepository.GetAsync(id, cancellationToken);
        if (profile == null)
        {
            return null;
        }

        return new UserReplica(
            profile.UserId,
            profile.Username ?? string.Empty,
            profile.DisplayName ?? string.Empty,
            profile.Avatar ?? string.Empty,
            false,
            null
        );
    }
}
