package com.arms.api.wbs.controller;

import com.arms.api.kafka.reqadd.model.ReqAddDTO;
import com.arms.api.util.response.CommonResponse;
import com.arms.api.wbs.model.vo.ReqAddAndWbsRowVO;
import com.arms.api.wbs.model.vo.WbsRowVO;
import com.arms.api.wbs.service.WbsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.List;

@Controller
@AllArgsConstructor
@Slf4j
@RequestMapping("/wbs")
public class WbsController {

    private final WbsService wbsService;

    @PostMapping
    @ResponseBody
    public Mono<CommonResponse.ApiResult<List<WbsRowVO>>> saveAllThenGetWbsRowVOS(
            @RequestBody ReqAddAndWbsRowVO reqAddAndWbsRowVO
    ) {
        return Mono.fromCallable(() -> {
            List<WbsRowVO> wbsRowVOS = wbsService.saveAllThenGetWbsRowVOS(reqAddAndWbsRowVO);
            return CommonResponse.success(wbsRowVOS);
        }).subscribeOn(Schedulers.boundedElastic());
    }

    @GetMapping("/pd-service-id/{serviceId}/id/{id}")
    @ResponseBody
    public Mono<CommonResponse.ApiResult<WbsRowVO>> findById(
            @PathVariable Long serviceId,
            @PathVariable String id
    ) {
        return Mono.fromCallable(() -> CommonResponse.success(wbsService.wbsRowVO(serviceId + ":" + id)))
                .subscribeOn(Schedulers.boundedElastic());
    }

    @GetMapping("/pd-service-id/{serviceId}")
    @ResponseBody
    public Mono<CommonResponse.ApiResult<List<WbsRowVO>>> findByServiceId(
            @PathVariable Long serviceId
    ) {
        return Mono.fromCallable(() -> CommonResponse.success(wbsService.findByServiceId(serviceId )))
                .subscribeOn(Schedulers.boundedElastic());
    }

    @DeleteMapping("/pd-service-id/{serviceId}")
    @ResponseBody
    public Mono<CommonResponse.ApiResult<String>> deleteById(
            @PathVariable Long serviceId
    ) {
        return Mono.fromCallable(() -> {
                        wbsService.deleteById(serviceId + ":*");
                        return CommonResponse.success("OK");
                    })
                .subscribeOn(Schedulers.boundedElastic());
    }

}
