package at.procon.ted.service.attachment;

import java.util.List;
import java.util.Set;

/**
 * Interface for attachment content extractors.
 * Each implementation handles a specific file format (PDF, ZIP, etc.).
 *
 * @author Martin.Schweitzer@procon.co.at and claude.ai
 */
public interface AttachmentExtractor {

    /**
     * Returns the set of file extensions this extractor can handle.
     * Extensions should be lowercase without the dot (e.g., "pdf", "zip").
     */
    Set<String> getSupportedExtensions();

    /**
     * Returns the set of MIME types this extractor can handle.
     */
    Set<String> getSupportedMimeTypes();

    /**
     * Check if this extractor can handle the given file.
     *
     * @param filename    the filename
     * @param contentType the MIME content type
     * @return true if this extractor can process the file
     */
    boolean canHandle(String filename, String contentType);

    /**
     * Extract content from the attachment.
     *
     * @param data        the raw file data
     * @param filename    the original filename
     * @param contentType the MIME content type
     * @return extraction result containing text and/or child attachments
     */
    ExtractionResult extract(byte[] data, String filename, String contentType);

    /**
     * Result of content extraction.
     */
    record ExtractionResult(
            /**
             * Extracted text content (for PDF, etc.).
             */
            String extractedText,
            /**
             * Child attachments (for ZIP files).
             */
            List<ChildAttachment> childAttachments,
            /**
             * Whether extraction was successful.
             */
            boolean success,
            /**
             * Error message if extraction failed.
             */
            String errorMessage
    ) {
        public static ExtractionResult success(String text) {
            return new ExtractionResult(text, List.of(), true, null);
        }

        public static ExtractionResult successWithChildren(List<ChildAttachment> children) {
            return new ExtractionResult(null, children, true, null);
        }

        public static ExtractionResult failure(String errorMessage) {
            return new ExtractionResult(null, List.of(), false, errorMessage);
        }
    }

    /**
     * Represents a child attachment extracted from a container (e.g., ZIP).
     */
    record ChildAttachment(
            String filename,
            String contentType,
            byte[] data,
            String pathInArchive
    ) {}
}
