package thyyxxk.webserver.controller.utilities;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.utilities.workorder.OrderInquiry;
import thyyxxk.webserver.entity.utilities.workorder.TakenInquiry;
import thyyxxk.webserver.entity.utilities.workorder.WorkOrder;
import thyyxxk.webserver.entity.utilities.workorder.WorkOrderDetail;
import thyyxxk.webserver.service.utilities.WorkOrderService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;

@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {
    private final WorkOrderService service;

    @Autowired
    public WorkOrderController(WorkOrderService service) {
        this.service = service;
    }

    @PostMapping("/submitNewOrder")
    public ResultVo<List<WorkOrder>> submitNewOrder(@RequestBody WorkOrder workOrder) {
        return ResultVoUtil.success(service.insertNewOrder(workOrder));
    }

    @GetMapping("/getMyOrders")
    public ResultVo<List<WorkOrder>> getMyOrders(){
        String creator = TokenUtil.getInstance().getTokenUserId();
        return ResultVoUtil.success(service.getAllOrdersByCreator(creator));
    }

    @PostMapping("/getAllOrders")
    public ResultVo<List<WorkOrder>> getAllOrders(@RequestBody OrderInquiry inquiry){
        return ResultVoUtil.success(service.getAllOrders(inquiry));
    }

    @PostMapping("/takeWorkOrder")
    public ResultVo<String> takeWorkOrder(@RequestBody WorkOrder workOrder) {
        return ResultVoUtil.success(service.takeWorkOrder(workOrder));
    }

    @PostMapping("/getMyTakenOrders")
    public ResultVo<List<WorkOrderDetail>> getMyTakenOrders(@RequestBody TakenInquiry inquiry) {
        return ResultVoUtil.success(service.getMyTakenOrders(inquiry));
    }

    @PostMapping("/updateOrderProgress")
    public ResultVo<String> updateOrderProgress(@RequestBody WorkOrderDetail detail) {
        return ResultVoUtil.success(service.updateOrderProgress(detail));
    }
}
