package com.eactive.eai.rms.onl.apim.portalpartnership;

import com.eactive.apim.portal.file.entity.FileInfo;
import com.eactive.apim.portal.file.service.FileService;
import com.eactive.apim.portal.partnershipapplication.entity.PartnershipApplication;
import com.eactive.apim.portal.portaluser.entity.PortalUser;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.data.entity.onl.apim.portalpartnerslhip.PortalPartnershipService;
import com.eactive.eai.rms.data.entity.onl.apim.portaluser.PortalUserService;
import com.eactive.eai.rms.onl.apim.masking.MaskingUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class PortalPartnershipManService extends BaseService {
	private final PortalPartnershipService partnershipService;
	private final PortalPartnershipUIMapper portalPartnershipUIMapper;
	private final PortalUserService portalUserService;
	private final FileService fileService;

	@Autowired
	public PortalPartnershipManService(PortalPartnershipService partnershipService,
									   PortalPartnershipUIMapper portalPartnershipUIMapper,
									   PortalUserService portalUserService, FileService fileService){
		this.partnershipService = partnershipService;
		this.portalPartnershipUIMapper = portalPartnershipUIMapper;
		this.portalUserService = portalUserService;
		this.fileService = fileService;
	}

	private PortalUser findUser(String createdBy) {
		return portalUserService.findByUserId(createdBy);
	}
	public Page<PortalPartnershipUI> selectList(Pageable pageable, String searchBizSubject, String searchBizDetail) {
		return partnershipService.findAll(pageable, searchBizSubject, searchBizDetail)
				.map(entity -> convertToUIWithMaskOption(entity, true));
	}

	public PortalPartnershipUI selectDetail(String id) {
		PartnershipApplication partnershipApplication = partnershipService.getById(id);
		return convertToUIWithMaskOption(partnershipApplication, true);
	}

	public PortalPartnershipUI selectDetailUnmask(String id) {
		PartnershipApplication partnershipApplication = partnershipService.getById(id);
		return convertToUIWithMaskOption(partnershipApplication, false);
	}

	private PortalPartnershipUI convertToUIWithMaskOption(PartnershipApplication partnership, boolean isMasked) {
		PortalPartnershipUI partnershipUI = portalPartnershipUIMapper.toVo(partnership);
		PortalUser user = findUser(partnership.getCreatedBy());

		if (isMasked) {
			// 마스킹 처리
			partnershipUI.setCreatedByName(MaskingUtils.maskName(user.getUserName()));
			partnershipUI.setCreatedById(MaskingUtils.maskEmailId(user.getLoginId()));
		} else {
			// 마스킹 해제 상태
			partnershipUI.setCreatedByName(user.getUserName());
			partnershipUI.setCreatedById(user.getLoginId());
		}

		// 첨부파일 처리
		if (StringUtils.isNotBlank(partnership.getFileId())) {
			FileInfo fileInfo = fileService.findById(partnership.getFileId());
			partnershipUI.setFileInfo(fileInfo);
			partnershipUI.setHasAttachment(true);
		} else {
			partnershipUI.setHasAttachment(false);
		}

		return partnershipUI;
	}

}
