package com.eactive.eai.authserver.dao;

import java.util.List;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.authserver.TokenIssuanceLog;

@Service
@Transactional
public class TokenIssuanceLogDAO extends BaseDAO {
    static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

    @Autowired
    private TokenIssuanceLogLoader tokenIssuanceLogLoader;

    public TokenIssuanceLog saveTokenIssuanceLog(TokenIssuanceLog log) throws DAOException {
        try {
            return tokenIssuanceLogLoader.save(log);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAUC002"));
        }
    }

    public List<TokenIssuanceLog> findByClientId(String clientId) throws DAOException {
        try {
            return tokenIssuanceLogLoader.findByClientId(clientId);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAUC003"));
        }
    }

    public List<TokenIssuanceLog> findByAppName(String appName) throws DAOException {
        try {
            return tokenIssuanceLogLoader.findByAppName(appName);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAUC004"));
        }
    }

    public List<TokenIssuanceLog> findByIssuanceDateTimeBetween(LocalDateTime start, LocalDateTime end) throws DAOException {
        try {
            return tokenIssuanceLogLoader.findByIssuanceDateTimeBetween(start, end);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAUC005"));
        }
    }

    public List<TokenIssuanceLog> findBySuccessYn(String successYn) throws DAOException {
        try {
            return tokenIssuanceLogLoader.findBySuccessYn(successYn);
        } catch (Exception e) {
            throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAUC006"));
        }
    }

    public int findRecentLogsForRestrictionCount(
            String clientId, LocalDateTime startDateTime) {
        return tokenIssuanceLogLoader.findRecentLogsForRestrictionCount(clientId, startDateTime);
    }
}