package com.eactive.testmaster.client.mapper;

import com.eactive.testmaster.client.dto.ApiCollectionDTO;
import com.eactive.testmaster.client.dto.ApiRequestDTO;
import com.eactive.testmaster.client.entity.ApiCollection;
import com.eactive.testmaster.client.entity.ApiRequestInfo;
import com.eactive.testmaster.client.repository.ApiRequestRepository;
import com.eactive.testmaster.common.mapper.CommonMapper;
import com.eactive.testmaster.server.mapper.ServerMapper;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Mapper(componentModel = "spring",
        uses = {CommonMapper.class, ServerMapper.class},
        unmappedTargetPolicy = ReportingPolicy.IGNORE)
@Component
public abstract class ApiRequestMapper {

    @Autowired
    private ApiRequestRepository apiRequestRepository;

    public abstract ApiCollection map(ApiCollectionDTO dto);

    public abstract ApiRequestInfo map(ApiRequestDTO dto);

    public abstract ApiRequestDTO map(ApiRequestInfo entity);

    public ApiRequestInfo map(String id) {
        if(id == null) {
            return null;
        }
        return apiRequestRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("No ApiRequest found for ID: " + id));
    }

    public abstract List<ApiRequestInfo> mapApis(List<ApiRequestDTO> apis);

    public abstract List<ApiCollectionDTO> mapCollections(List<ApiCollection> apis);
}
