package com.ffii.fhsmsc.modules.user.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ffii.core.support.AbstractBaseEntityService;
import com.ffii.core.support.JdbcDao;
import com.ffii.fhsmsc.modules.common.SettingNames;
import com.ffii.fhsmsc.modules.settings.service.SettingsService;
import com.ffii.fhsmsc.modules.user.entity.UserPasswordHistory;
import com.ffii.fhsmsc.modules.user.entity.UserPasswordHistoryRepository;

@Service
public class UserPasswordHistoryService extends AbstractBaseEntityService<UserPasswordHistory, Long, UserPasswordHistoryRepository> {
	private SettingsService settingsService;

    public UserPasswordHistoryService(JdbcDao jdbcDao, UserPasswordHistoryRepository repository, SettingsService settingsService) {
		super(jdbcDao, repository);
		this.settingsService = settingsService;
	}

	public List<String> getPasswordHistory(long userId) {
		int passHist = settingsService.getInt(SettingNames.SYS_PASSWORD_RULE_HISTORY);
		StringBuilder sql = new StringBuilder(" SELECT password FROM user_password_history WHERE deleted = 0 AND userId = :userId ORDER BY date DESC LIMIT " + passHist);
		return jdbcDao.queryForStrings(sql.toString(), Map.of("userId", userId));
	}

}