package com.agent.management.rag.controller;

import com.agent.management.common.Result;
import com.agent.management.model.entity.RagQueryExample;
import com.agent.management.rag.capability.RagCapabilityProfile;
import com.agent.management.rag.capability.RagCapabilityProfileService;
import com.agent.management.rag.memory.RagExampleMemoryService;
import com.agent.management.rag.model.RagSourceType;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/rag/capabilities")
@RequiredArgsConstructor
public class RagCapabilityController {
    private final RagCapabilityProfileService profiles;
    private final RagExampleMemoryService examples;
    private final com.agent.management.rag.capability.RagGovernanceService governance;
    private final com.agent.management.rag.memory.RagExampleCandidateService candidateImports;
    private final com.agent.management.rag.capability.GraphOnboardingAssessmentService graphAssessment;
    private final com.agent.management.rag.capability.GraphGovernanceConfigService graphGovernance;

    @GetMapping("/{type}/{sourceId}")
    public Result<RagCapabilityProfile> get(@PathVariable RagSourceType type, @PathVariable String sourceId) {
        return Result.success(profiles.get(type, sourceId));
    }

    @PostMapping("/{type}/{sourceId}/refresh")
    public Result<RagCapabilityProfile> refresh(@PathVariable RagSourceType type, @PathVariable String sourceId) {
        return Result.success(profiles.refresh(type, sourceId));
    }

    @GetMapping("/{type}/{sourceId}/examples")
    public Result<List<RagQueryExample>> examples(@PathVariable RagSourceType type, @PathVariable String sourceId) {
        return Result.success(examples.list(type, sourceId));
    }

    @PostMapping("/{type}/{sourceId}/examples/candidates")
    public Result<java.util.Map<String,Object>> addCandidates(@PathVariable RagSourceType type, @PathVariable String sourceId,
                                                               @RequestBody List<java.util.Map<String,Object>> candidates) {
        return Result.success(candidateImports.importValidated(type, sourceId, candidates));
    }

    @PutMapping("/examples/{id}/verified")
    public Result<RagQueryExample> verify(@PathVariable Long id, @RequestParam boolean value) {
        return Result.success(examples.setVerified(id, value));
    }

    @DeleteMapping("/examples/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        examples.delete(id);
        return Result.success(null);
    }

    @PostMapping("/{type}/{sourceId}/suggest")
    public Result<java.util.Map<String,Object>> suggest(@PathVariable RagSourceType type,@PathVariable String sourceId,
                                                        @RequestBody(required=false) java.util.Map<String,Object> body) {
        String description=body==null?"":String.valueOf(body.getOrDefault("description",""));
        return Result.success(governance.suggest(type,sourceId,description));
    }

    @GetMapping("/GRAPH/{sourceId}/readiness")
    public Result<java.util.Map<String,Object>> graphReadiness(@PathVariable String sourceId) {
        return Result.success(graphAssessment.assess(sourceId));
    }

    @GetMapping("/GRAPH/{sourceId}/governance")
    public Result<java.util.Map<String,Object>> graphGovernance(@PathVariable Long sourceId) {
        return Result.success(graphGovernance.get(sourceId));
    }

    @PutMapping("/GRAPH/{sourceId}/governance")
    public Result<java.util.Map<String,Object>> updateGraphGovernance(
            @PathVariable Long sourceId, @RequestBody java.util.Map<String,Object> config) {
        return Result.success(graphGovernance.save(sourceId, config));
    }
}
