package com.eactive.eai.rms.common.acl.audit.adapter.out.persistence;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import com.eactive.eai.rms.common.acl.audit.domain.AuditLog;
import com.eactive.eai.rms.common.acl.audit.port.in.SelectListAuditLogCommand;
import com.eactive.eai.rms.common.acl.audit.port.out.SaveAuditLogPort;
import com.eactive.eai.rms.common.acl.audit.port.out.SelectDetailAuditLogPort;
import com.eactive.eai.rms.common.acl.audit.port.out.SelectListAuditLogPort;
import com.eactive.eai.rms.data.entity.man.audit.AuditLogEntity;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
class AuditLogPersistenceAdapter implements SelectListAuditLogPort, SelectDetailAuditLogPort, SaveAuditLogPort {

	private final AuditLogRepository auditLogRepository;
	private final AuditLogMapper auditLogMapper;

	@Override
	public Page<AuditLog> findPage(Pageable pageable, SelectListAuditLogCommand command) {
		return auditLogRepository.findPage(pageable, command).map(auditLogMapper::toDomain);
	}

	@Override
	public AuditLog getById(Long id) {
		return auditLogMapper.toDomain(auditLogRepository.getReferenceById(id));
	}

	@Override
	public AuditLog save(AuditLog auditLog) {
		AuditLogEntity auditLogEntity = auditLogMapper.toEntity(auditLog);
		Long id = auditLogRepository.save(auditLogEntity).getId();
		auditLog.setId(id);
		return auditLog;
	}

}
