package com.ai.trackwalker.api;

import com.ai.trackwalker.api.dto.Requests;
import com.ai.trackwalker.service.PlatformService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class PlatformController {
    private final PlatformService service;

    public PlatformController(PlatformService service) {
        this.service = service;
    }

    @PostMapping("/inspection/tasks")
    public ApiResponse<?> createTask(@Valid @RequestBody Requests.CreateTaskRequest request) {
        return ApiResponse.ok(service.createTask(request));
    }

    @GetMapping("/inspection/tasks")
    public ApiResponse<?> listTasks() {
        return ApiResponse.ok(Map.of("tasks", service.listTasks()));
    }

    @PostMapping("/inspection/tasks/{taskId}/cancel")
    public ApiResponse<?> cancelTask(@PathVariable String taskId) {
        return ApiResponse.ok(service.cancelTask(taskId));
    }

    @PostMapping("/uav/callbacks/mission-status")
    public ApiResponse<?> missionStatus(@RequestBody Requests.MissionStatusCallback callback) {
        return ApiResponse.ok(service.updateMissionStatus(callback));
    }

    @PostMapping("/inspection/resources/complete")
    public ApiResponse<?> completeResources(@Valid @RequestBody Requests.ResourceCompleteRequest request) {
        return ApiResponse.ok(service.completeResources(request));
    }

    @GetMapping("/inspection/resources")
    public ApiResponse<?> resources() {
        return ApiResponse.ok(Map.of("resources", service.listResources()));
    }

    @PostMapping("/analysis/jobs")
    public ApiResponse<?> createAnalysisJob(@RequestBody Requests.CreateAnalysisJobRequest request) {
        return ApiResponse.ok(service.createAnalysisJob(request));
    }

    @PostMapping("/analysis/jobs/{jobId}/run")
    public ApiResponse<?> runAnalysis(@PathVariable String jobId) {
        return ApiResponse.ok(service.runAnalysis(jobId));
    }

    @GetMapping("/analysis/jobs")
    public ApiResponse<?> analysisJobs() {
        return ApiResponse.ok(Map.of("jobs", service.listAnalysisJobs()));
    }

    @GetMapping("/analysis/results")
    public ApiResponse<?> analysisResults() {
        return ApiResponse.ok(Map.of("results", service.listAnalysisResults()));
    }

    @GetMapping("/alarms")
    public ApiResponse<?> alarms(@RequestParam(defaultValue = "false") boolean includeSuppressed) {
        return ApiResponse.ok(Map.of("alarms", service.listAlarms(includeSuppressed)));
    }

    @PostMapping("/alarms/{alarmId}/decision")
    public ApiResponse<?> decideAlarm(@PathVariable String alarmId, @RequestBody Requests.AlarmDecisionRequest request) {
        return ApiResponse.ok(service.decideAlarm(alarmId, request));
    }

    @GetMapping("/workorders")
    public ApiResponse<?> workorders() {
        return ApiResponse.ok(Map.of("workorders", service.listWorkOrders()));
    }

    @PostMapping("/workorders/{workorderId}/redispatch")
    public ApiResponse<?> redispatchWorkorder(@PathVariable String workorderId, @RequestBody Map<String, String> request) {
        return ApiResponse.ok(service.redispatchWorkOrder(
                workorderId,
                request.get("assignee"),
                request.get("reason"),
                request.get("operator")
        ));
    }

    @PostMapping("/workorders/{workorderId}/review")
    public ApiResponse<?> reviewWorkorder(@PathVariable String workorderId, @RequestBody Requests.WorkOrderReviewRequest request) {
        return ApiResponse.ok(service.reviewWorkOrder(workorderId, request));
    }

    @PostMapping("/workorders/callbacks/status")
    public ApiResponse<?> workOrderCallback(@RequestBody Requests.WorkOrderCallbackRequest request) {
        return ApiResponse.ok(service.workOrderCallback(request));
    }

    @PostMapping("/exemptions")
    public ApiResponse<?> createExemption(@RequestBody Requests.ExemptionRequest request) {
        return ApiResponse.ok(service.createExemption(request));
    }

    @GetMapping("/inspection/resources/{resourceId}/preview")
    public ApiResponse<?> resourcePreview(@PathVariable String resourceId) {
        return ApiResponse.ok(service.resourcePreview(resourceId));
    }

    @GetMapping("/analysis/results/{resultId}/evidence")
    public ApiResponse<?> resultEvidence(@PathVariable String resultId) {
        return ApiResponse.ok(service.resultEvidence(resultId));
    }

    @GetMapping("/alarms/{alarmId}/evidence-chain")
    public ApiResponse<?> alarmEvidence(@PathVariable String alarmId) {
        return ApiResponse.ok(service.alarmEvidenceChain(alarmId));
    }

    @PostMapping("/samples/{sampleId}/decision")
    public ApiResponse<?> sampleDecision(@PathVariable String sampleId, @RequestBody Requests.SampleDecisionRequest request) {
        return ApiResponse.ok(service.decideSample(sampleId, request));
    }

    @GetMapping("/samples")
    public ApiResponse<?> samples() {
        return ApiResponse.ok(Map.of("samples", service.listSamples()));
    }

    @GetMapping("/platform/events")
    public ApiResponse<?> platformEvents() {
        return ApiResponse.ok(Map.of("events", service.listPlatformEvents()));
    }

    @GetMapping("/operations/ai-runtime")
    public ApiResponse<?> aiRuntimeStatus() {
        return ApiResponse.ok(service.aiRuntimeStatus());
    }

    @GetMapping("/stats/overview")
    public ApiResponse<?> overview() {
        return ApiResponse.ok(service.overview());
    }

    @PostMapping("/demo/run-full-chain")
    public ApiResponse<?> runDemo() {
        return ApiResponse.ok(service.runDemo());
    }
}
