package at.procon.dip.embedding.web;

import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.RepresentationType;
import at.procon.dip.embedding.service.ScopedEmbeddingEnqueueResult;
import at.procon.dip.embedding.service.ScopedEmbeddingEnqueueService;
import at.procon.dip.runtime.condition.ConditionalOnRuntimeMode;
import at.procon.dip.runtime.config.RuntimeMode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController
@ConditionalOnRuntimeMode(RuntimeMode.NEW)
@RequestMapping("/v1/dip/admin/embeddings")
@RequiredArgsConstructor
@Tag(name = "Embedding Admin", description = "Administrative operations for NEW-runtime representation embeddings")
public class EmbeddingAdminController {

    private final ScopedEmbeddingEnqueueService enqueueService;

    @PostMapping("/enqueue-by-document-type")
    @Operation(
            summary = "Queue embeddings by document type",
            description = "Queues embedding jobs only for DOC text representations belonging to the requested document type."
    )
    public ScopedEmbeddingEnqueueResult enqueueByDocumentType(
            @RequestParam String documentType,
            @RequestParam(required = false) String representationType,
            @RequestParam(required = false) String builderKey,
            @RequestParam(required = false, defaultValue = "false") boolean primaryOnly,
            @RequestParam(required = false) String modelKey,
            @RequestParam(required = false, defaultValue = "false") boolean force,
            @RequestParam(required = false, defaultValue = "1000") int limit) {
        return enqueueService.enqueueByDocumentType(
                parseEnum(DocumentType.class, documentType, "documentType"),
                parseEnum(RepresentationType.class, representationType, "representationType"),
                builderKey,
                primaryOnly,
                modelKey,
                force,
                limit
        );
    }

    private <E extends Enum<E>> E parseEnum(Class<E> enumType, String value, String parameterName) {
        if (value == null || value.isBlank()) {
            return null;
        }
        String normalized = value.trim().replace('-', '_').toUpperCase(java.util.Locale.ROOT);
        try {
            return Enum.valueOf(enumType, normalized);
        } catch (IllegalArgumentException ex) {
            throw new ResponseStatusException(
                    HttpStatus.BAD_REQUEST,
                    "Invalid " + parameterName + ": " + value
            );
        }
    }
}
