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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectContextRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectCreateWithTeamReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectGroupPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectGroupPageRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectOverviewSummaryRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.project.ProjectStatusActionReqVO;
import com.njcn.rdms.module.project.service.project.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

@Tag(name = "管理后台 - 项目管理")
@RestController
@RequestMapping("/project/project")
@Validated
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('project:project:create')")
    public CommonResult<Long> createProject(@Valid @RequestBody ProjectSaveReqVO createReqVO) {
        return success(projectService.createProject(createReqVO));
    }

    @PostMapping("/create-with-team")
    @Operation(summary = "创建项目并初始化团队（原子接口）")
    @PreAuthorize("@ss.hasPermission('project:project:create')")
    public CommonResult<Long> createProjectWithTeam(@Valid @RequestBody ProjectCreateWithTeamReqVO reqVO) {
        return success(projectService.createProjectWithTeam(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    public CommonResult<Boolean> updateProject(@Valid @RequestBody ProjectSaveReqVO updateReqVO) {
        projectService.updateProject(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取项目详情")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<ProjectRespVO> getProject(@RequestParam("id") Long id) {
        return success(projectService.getProjectDetail(id));
    }

    @GetMapping("/{id}/context")
    @Operation(summary = "获取项目上下文")
    @Parameter(name = "id", description = "项目编号", required = true, example = "1024")
    public CommonResult<ProjectContextRespVO> getProjectContext(@PathVariable("id") Long id) {
        return success(projectService.getProjectContext(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获取项目分页")
    public CommonResult<PageResult<ProjectRespVO>> getProjectPage(@Valid ProjectPageReqVO pageReqVO) {
        return success(projectService.getProjectPage(pageReqVO));
    }

    @GetMapping("/group-page")
    @Operation(summary = "获取项目按产品分组分页")
    public CommonResult<ProjectGroupPageRespVO> getProjectGroupPage(@Valid ProjectGroupPageReqVO reqVO) {
        // 与 page / overview-summary 一致：全域读路径不挂权限注解，可见性由 Service 内 ObjectDataScope 数据权限承载
        return success(projectService.getProjectGroupPage(reqVO));
    }

    @GetMapping("/list-by-product")
    @Operation(summary = "根据产品编号获取该产品下的全部项目")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<List<ProjectRespVO>> getProjectListByProductId(@RequestParam("productId") Long productId) {
        return success(projectService.getProjectListByProductId(productId));
    }

    @GetMapping("/overview-summary")
    @Operation(summary = "获取项目入口页概览统计")
    public CommonResult<ProjectOverviewSummaryRespVO> getProjectOverviewSummary() {
        return success(projectService.getProjectOverviewSummary());
    }

    @PostMapping("/change-status")
    @Operation(summary = "变更项目状态")
    public CommonResult<Boolean> changeProjectStatus(@Valid @RequestBody ProjectStatusActionReqVO reqVO) {
        projectService.changeProjectStatus(reqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除项目")
    public CommonResult<Boolean> deleteProject(@Valid @RequestBody ProjectDeleteReqVO reqVO) {
        projectService.deleteProject(reqVO);
        return success(true);
    }

}
