package com.huimv.employment.controller.mp;

import com.huimv.employment.common.exception.BizException;
import com.huimv.employment.common.exception.ErrorCode;
import com.huimv.employment.common.web.R;
import com.huimv.employment.security.LoginUser;
import com.huimv.employment.security.LoginUserHolder;
import com.huimv.employment.service.order.EmploymentOrderService;
import com.huimv.employment.service.order.dto.OrderStartWorkRequest;
import com.huimv.employment.service.order.dto.OrderStartWorkResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企业端用工订单：开工确认等。
 */
@RestController
@RequestMapping("/api/v1/mp/orders")
@Tag(name = "用工订单", description = "企业确认开工等")
@SecurityRequirement(name = "Authorization")
public class EmploymentOrderController {

    private static final String USER_TYPE_ENTERPRISE = "enterprise";

    private final EmploymentOrderService employmentOrderService;

    public EmploymentOrderController(EmploymentOrderService employmentOrderService) {
        this.employmentOrderService = employmentOrderService;
    }

    @GetMapping("/{orderId}/start-work-status")
    @Operation(summary = "查询开工就绪状态",
            description = "返回已签/计划人数、是否齐签、是否缺编、是否可确认开工。待审人数仅提示，不阻挡。",
            parameters = {
                    @Parameter(name = "orderId", in = ParameterIn.PATH, required = true,
                            description = "fe_employment_order.id")
            })
    public R<OrderStartWorkResponse> startWorkStatus(@PathVariable("orderId") Long orderId) {
        requireEnterprise();
        return R.ok(employmentOrderService.getStartWorkStatus(LoginUserHolder.require().getUserId(), orderId));
    }

    @PostMapping("/{orderId}/start-work")
    @Operation(summary = "确认开工",
            description = "企业确认开工。须已过审工人全部签完；已签人数 &lt; 计划人数时须 allow_understaffed=true。"
                    + "成功后订单 contract_signing→work_started。",
            parameters = {
                    @Parameter(name = "orderId", in = ParameterIn.PATH, required = true,
                            description = "fe_employment_order.id")
            })
    public R<OrderStartWorkResponse> startWork(@PathVariable("orderId") Long orderId,
                                               @RequestBody(required = false) OrderStartWorkRequest request) {
        requireEnterprise();
        if (request == null) {
            request = new OrderStartWorkRequest();
        }
        return R.ok(employmentOrderService.startWork(LoginUserHolder.require().getUserId(), orderId, request));
    }

    private static LoginUser requireEnterprise() {
        LoginUser loginUser = LoginUserHolder.require();
        if (!USER_TYPE_ENTERPRISE.equals(loginUser.getUserType())) {
            throw new BizException(ErrorCode.ENTERPRISE_ONLY);
        }
        return loginUser;
    }
}
