package com.eactive.testmaster.client.service;


import com.eactive.testmaster.client.entity.ApiLayout;
import com.eactive.testmaster.client.entity.ApiRequestInfo;
import com.eactive.testmaster.client.repository.ApiLayoutRepository;
import com.eactive.testmaster.client.repository.ApiRequestRepository;
import com.eactive.testmaster.common.exception.NotFoundException;
import java.util.UUID;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class ApiLayoutMgmtService {

    private final ApiLayoutRepository apiLayoutRepository;

    private final ApiRequestRepository apiRequestRepository;

    public ApiLayoutMgmtService(ApiRequestRepository apiRequestRepository, ApiLayoutRepository apiLayoutRepository) {
        this.apiRequestRepository = apiRequestRepository;
        this.apiLayoutRepository = apiLayoutRepository;
    }

    public ApiLayout findLayoutByApiRequest(String apiRequestId) {
        ApiRequestInfo apiRequestInfo = apiRequestRepository.findById(apiRequestId)
            .orElseThrow(() -> new NotFoundException(String.format("ApiRequestInfo with ID %s not found", apiRequestId)));

        return apiLayoutRepository.findFirstByApiRequestInfo(apiRequestInfo);
    }

    public ApiLayout saveLayout(ApiLayout layout) {
        ApiLayout apiLayout = apiLayoutRepository.findFirstByApiRequestInfo(layout.getApiRequestInfo());

        return apiLayout == null ? createLayout(layout) : updateLayout(apiLayout.getId(), layout);
    }

    public ApiLayout createLayout(ApiLayout layout) {
        layout.setId(UUID.randomUUID().toString());
        if (layout.getLayoutItems() != null && !layout.getLayoutItems().isEmpty()) {
            layout.getLayoutItems().forEach(layoutItem -> {
                layoutItem.setApiLayout(layout);
            });
        }
        return apiLayoutRepository.save(layout);
    }

    public ApiLayout updateLayout(String layoutId, ApiLayout updatedLayout) {
        ApiLayout layout = apiLayoutRepository.findById(layoutId)
            .orElseThrow(() -> new NotFoundException(String.format("Layout with ID %s not found", layoutId)));

        // Update fields based on the values provided in updatedLayout
        layout.setLoutName(updatedLayout.getLoutName());
        layout.setLoutDesc(updatedLayout.getLoutDesc());
        layout.setLoutPtrnName(updatedLayout.getLoutPtrnName());
        layout.setLoutDesc(updatedLayout.getLoutDesc());
        layout.setEaiBzwkDstCd(updatedLayout.getEaiBzwkDstCd());
        layout.setUApplName(updatedLayout.getUApplName());
        layout.setSysIntFacName(updatedLayout.getSysIntFacName());
        layout.setAuthor(updatedLayout.getAuthor());
        layout.setEaiSevRdStCd(updatedLayout.getEaiSevRdStCd());
        layout.setModfMgtStusDstCd(updatedLayout.getModfMgtStusDstCd());
        layout.setUseYn(updatedLayout.getUseYn());
        layout.setVerInfo(updatedLayout.getVerInfo());
        layout.setComputedLayoutItemRoot(updatedLayout.getComputedLayoutItemRoot());

        if (updatedLayout.getLayoutItems() != null && !updatedLayout.getLayoutItems().isEmpty()) {
            layout.getLayoutItems().clear();
            updatedLayout.getLayoutItems().forEach(layoutItem -> {
                layoutItem.setApiLayout(layout); // Set back reference to layout
                layout.getLayoutItems().add(layoutItem);
            });
        }

        return apiLayoutRepository.save(layout);
    }

    // Method to delete a layout
    public void deleteLayout(String id) {
        ApiLayout layout = apiLayoutRepository.findById(id)
            .orElseThrow(() -> new NotFoundException(String.format("Layout with ID %s not found", id)));
        apiLayoutRepository.delete(layout);
    }
}
