package at.procon.dip.domain.document.service;

import at.procon.dip.domain.document.entity.DocumentSource;
import at.procon.dip.domain.document.repository.DocumentSourceRepository;
import at.procon.dip.domain.document.service.command.AddDocumentSourceCommand;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class DocumentSourceService {

    private final DocumentService documentService;
    private final DocumentSourceRepository sourceRepository;

    public DocumentSource addSource(AddDocumentSourceCommand command) {
        DocumentSource source = DocumentSource.builder()
                .document(documentService.getRequired(command.documentId()))
                .sourceType(command.sourceType())
                .externalSourceId(command.externalSourceId())
                .sourceUri(command.sourceUri())
                .sourceFilename(command.sourceFilename())
                .parentSourceId(command.parentSourceId())
                .importBatchId(command.importBatchId())
                .receivedAt(command.receivedAt())
                .build();
        return sourceRepository.save(source);
    }

    @Transactional(readOnly = true)
    public List<DocumentSource> findByDocument(UUID documentId) {
        return sourceRepository.findByDocument_Id(documentId);
    }
}
