package com.rtlabs.users.service;

import com.rtlabs.users.auth.AppUser;
import com.rtlabs.users.domain.UserProfile;
import org.springframework.stereotype.Service;

@Service
public class UserProfileService {

  private final UserProfileRepository repository;
  private final ProfileAuditLogger auditLogger;

  public UserProfileService(UserProfileRepository repository,
                            ProfileAuditLogger auditLogger) {
    this.repository = repository;
    this.auditLogger = auditLogger;
  }

  public UserProfile loadProfile(Long id, AppUser principal) {
    UserProfile profile = repository.findById(id)
        .orElseThrow(() -> new ProfileNotFoundException(id));

    auditLogger.logRead(principal.getUsername(), id);
    return profile;
  }
}
