package com.njcn.rdms.module.project.controller.admin.project.project;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageParam;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.project.vo.myproject.MyProjectOwnedRespVO;
import com.njcn.rdms.module.project.controller.admin.project.project.vo.myproject.MyProjectPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.project.vo.myproject.MyProjectParticipatedRespVO;
import com.njcn.rdms.module.project.service.project.MyProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工作台「我的项目」")
@RestController
@RequestMapping("/project/project/me")
@Validated
public class MyProjectController {

    @Resource
    private MyProjectService myProjectService;

    @GetMapping("/participated/page")
    @Operation(summary = "分页获取当前登录用户参与的项目（作为成员）")
    public CommonResult<PageResult<MyProjectParticipatedRespVO>> getMyParticipatedPage(@Valid MyProjectPageReqVO reqVO) {
        normalizePageSize(reqVO);
        return success(myProjectService.getMyParticipatedPage(reqVO));
    }

    @GetMapping("/owned/page")
    @Operation(summary = "分页获取当前登录用户负责的项目（managerUserId=当前用户）")
    public CommonResult<PageResult<MyProjectOwnedRespVO>> getMyOwnedPage(@Valid MyProjectPageReqVO reqVO) {
        normalizePageSize(reqVO);
        return success(myProjectService.getMyOwnedPage(reqVO));
    }

    /** 前端固定传 pageSize=-1 拉全部；负数统一归一为 PAGE_SIZE_NONE，与 MyExecutionController 一致。 */
    private void normalizePageSize(MyProjectPageReqVO reqVO) {
        if (reqVO.getPageSize() != null && reqVO.getPageSize() < 0) {
            reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
    }

}
