package com.sino.mci.flow.controller;

import com.sino.mci.flow.dto.CreateFlowRequest;
import com.sino.mci.flow.service.FlowDefinitionService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/inbound/flow")
@RequiredArgsConstructor
public class FlowDefinitionController {

    private final FlowDefinitionService flowDefinitionService;

    @PostMapping
    public ApiResponse<?> createFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody CreateFlowRequest request) {
        return ApiResponse.ok(flowDefinitionService.createFlow(tenantCode, request));
    }

    @GetMapping("/{flow_code}")
    public ApiResponse<?> getFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("flow_code") String flowCode) {
        return ApiResponse.ok(flowDefinitionService.getFlow(tenantCode, flowCode));
    }

    @GetMapping("/list")
    public ApiResponse<?> listFlows(@RequestHeader("X-Tenant-Code") String tenantCode) {
        return ApiResponse.ok(flowDefinitionService.listFlows(tenantCode));
    }

    @PostMapping("/{flow_code}")
    public ApiResponse<?> updateFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("flow_code") String flowCode,
            @Valid @RequestBody CreateFlowRequest request) {
        return ApiResponse.ok(flowDefinitionService.updateFlow(tenantCode, flowCode, request));
    }

    @PostMapping("/{flow_code}/publish")
    public ApiResponse<?> publishFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("flow_code") String flowCode) {
        flowDefinitionService.publishFlow(tenantCode, flowCode);
        return ApiResponse.ok();
    }

    @PostMapping("/{flow_code}/offline")
    public ApiResponse<?> offlineFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("flow_code") String flowCode) {
        flowDefinitionService.offlineFlow(tenantCode, flowCode);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{flow_code}")
    public ApiResponse<?> deleteFlow(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("flow_code") String flowCode) {
        flowDefinitionService.deleteFlow(tenantCode, flowCode);
        return ApiResponse.ok();
    }
}
