package ch.unisg.executorpool.adapter.out.persistence.mongodb;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ch.unisg.executorpool.application.port.out.AddExecutorPort;
import ch.unisg.executorpool.application.port.out.LoadExecutorPort;
import ch.unisg.executorpool.application.port.out.RemoveExecutorPort;
import ch.unisg.executorpool.domain.ExecutorClass;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ExecutorPersistenceAdapter implements AddExecutorPort, RemoveExecutorPort, LoadExecutorPort {
    
    @Autowired
    private final ExecutorRepository executorRepository;

    private final ExecutorMapper executorMapper;

    @Override
    public void addExecutor(ExecutorClass executorClass) {
        MongoExecutorDocument mongoExecutorDocument = executorMapper.mapToMongoDocument(executorClass);
        executorRepository.save(mongoExecutorDocument);
    }

    @Override
    public void removeExecutor(ExecutorClass executorClass) {
        MongoExecutorDocument mongoExecutorDocument = executorMapper.mapToMongoDocument(executorClass);
        executorRepository.delete(mongoExecutorDocument);
    }

    @Override
    public ExecutorClass loadExecutor(ExecutorClass.ExecutorUri executorUri, ExecutorClass.ExecutorTaskType executorTaskType) {
        MongoExecutorDocument mongoExecutorDocument = executorRepository.findByExecutorUri(executorUri.getValue().toString(), executorTaskType.getValue());
        ExecutorClass executorClass = executorMapper.mapToDomainEntity(mongoExecutorDocument);
        return executorClass;
    }

}
