package com.ai.trackwalker.api;

import com.ai.trackwalker.api.dto.ModelManagementRequests;
import com.ai.trackwalker.model.ModelRuntimeDeploymentService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

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

    public ModelRuntimeDeploymentController(ModelRuntimeDeploymentService service) {
        this.service = service;
    }

    @GetMapping("/models/{modelGroupId}/runtime-deployments")
    public ApiResponse<?> deployments(@PathVariable String modelGroupId) {
        return ApiResponse.ok(Map.of("deployments", service.list(modelGroupId)));
    }

    @GetMapping("/model-runtime-deployments/active")
    public ApiResponse<?> activeDeployments() {
        return ApiResponse.ok(Map.of("deployments", service.activeDeployments()));
    }

    @PostMapping("/models/{modelGroupId}/runtime-deployments")
    public ApiResponse<?> create(
            @PathVariable String modelGroupId,
            @Valid @RequestBody ModelManagementRequests.SaveRuntimeDeploymentRequest request
    ) {
        return ApiResponse.ok(service.create(modelGroupId, request));
    }

    @PutMapping("/model-runtime-deployments/{deploymentId}")
    public ApiResponse<?> update(
            @PathVariable String deploymentId,
            @Valid @RequestBody ModelManagementRequests.SaveRuntimeDeploymentRequest request
    ) {
        return ApiResponse.ok(service.update(deploymentId, request));
    }

    @DeleteMapping("/model-runtime-deployments/{deploymentId}")
    public ApiResponse<?> delete(
            @PathVariable String deploymentId,
            @RequestParam(required = false) String operator
    ) {
        return ApiResponse.ok(service.delete(deploymentId, operator));
    }

    @PostMapping("/model-runtime-deployments/{deploymentId}/health-check")
    public ApiResponse<?> healthCheck(@PathVariable String deploymentId) {
        return ApiResponse.ok(service.checkHealth(deploymentId));
    }

    @PostMapping("/model-runtime-deployments/{deploymentId}/connectivity-check")
    public ApiResponse<?> connectivityCheck(@PathVariable String deploymentId) {
        return ApiResponse.ok(service.checkConnectivity(deploymentId));
    }

    @PostMapping("/model-runtime-deployments/{deploymentId}/activate")
    public ApiResponse<?> activate(
            @PathVariable String deploymentId,
            @RequestBody(required = false) ModelManagementRequests.SwitchRuntimeDeploymentRequest request
    ) {
        return ApiResponse.ok(service.activate(deploymentId, request));
    }
}
