using Knot.Shared.Kernel;
using Knot.Contracts.Profiles.Domain;
using Knot.Contracts.Profiles.Application.DTOs;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Knot.Modules.Profiles.Application.Profiles.UpdateSettings;

public sealed record UpdateSettingsCommand(Guid UserId, bool? HideStoryViews) : ICommand<UserProfileDto>;

internal sealed class UpdateSettingsCommandHandler : ICommandHandler<UpdateSettingsCommand, UserProfileDto>
{
    private readonly IProfileRepository _repository;

    public UpdateSettingsCommandHandler(IProfileRepository repository)
    {
        _repository = repository;
    }

    public async Task<Result<UserProfileDto>> Handle(UpdateSettingsCommand request, CancellationToken cancellationToken)
    {
        var profile = await _repository.GetAsync(request.UserId, cancellationToken);
        if (profile is null)
            return Result.Failure<UserProfileDto>(ProfilesErrors.ProfileNotFound);

        // Settings update logic would go here
        var result = await _repository.UpdateAsync(profile, cancellationToken);
        if (result.IsFailure)
            return Result.Failure<UserProfileDto>(result.Error);

        return Result.Success(result.Value);
    }
}
