package com.eactive.eai.rms.onl.apim.approval.apprequest;

import com.eactive.apim.portal.apispec.service.ApiSpecInfoService;
import com.eactive.apim.portal.apprequest.entity.AppRequest;
import com.eactive.eai.rms.common.base.BaseService;
import lombok.RequiredArgsConstructor;
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")
@RequiredArgsConstructor
public class PortalAppRequestManService extends BaseService {

    private final ApiSpecInfoService apiSpecInfoService;
    private final PortalAppRequestService portalAppRequestService;
    private final AppRequestUIMapper appRequestUIMapper;

    public Page<AppRequestUI> selectList(Pageable pageable, AppRequestUISearch appRequestUISearch) {
        return portalAppRequestService.findAllProd(pageable, appRequestUISearch)
            .map(appRequest -> appRequestUIMapper.toVo(appRequest));
    }

    public AppRequestUI selectDetail(String id) {
        AppRequest appRequest = portalAppRequestService.getById(id);

        return appRequestUIMapper.toVo(appRequest);
    }

    public AppRequestUI update(AppRequestUI appRequestUI) {
        AppRequest appRequest = portalAppRequestService.getById(appRequestUI.getId());
        appRequestUIMapper.updateToEntity(appRequestUI, appRequest);

        appRequest = portalAppRequestService.save(appRequest);
        return appRequestUIMapper.toVo(appRequest);
    }
}
