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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.common.util.object.BeanUtils;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductContextRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductCreateWithTeamReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductOptionRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductOverviewSummaryRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductPageReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.product.ProductStatusActionReqVO;
import com.njcn.rdms.module.project.dal.dataobject.product.ProductDO;
import com.njcn.rdms.module.project.service.product.ProductService;
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/product")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('project:product:create')")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

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

    @PutMapping("/update")
    @Operation(summary = "更新产品")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取产品详情")
    @Parameter(name = "id", description = "产品编号", required = true, example = "1024")
    public CommonResult<ProductRespVO> getProduct(@RequestParam("id") Long id) {
        ProductDO product = productService.getProduct(id);
        return success(BeanUtils.toBean(product, ProductRespVO.class));
    }

    @GetMapping("/{id}/context")
    @Operation(summary = "获取产品上下文")
    @Parameter(name = "id", description = "产品编号", required = true, example = "1024")
    public CommonResult<ProductContextRespVO> getProductContext(@PathVariable("id") Long id) {
        return success(productService.getProductContext(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获取产品分页")
    public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) {
        // VO 组装（含当前用户角色聚合）已下沉到 Service，Controller 直接返回
        return success(productService.getProductPage(pageReqVO));
    }

    @GetMapping("/overview-summary")
    @Operation(summary = "获取产品入口页概览统计")
    public CommonResult<ProductOverviewSummaryRespVO> getProductOverviewSummary() {
        return success(productService.getProductOverviewSummary());
    }

    @GetMapping("/options")
    @Operation(summary = "获取可绑定产品下拉选项")
    public CommonResult<List<ProductOptionRespVO>> getProductOptions() {
        return success(productService.getProductOptions());
    }

    @PostMapping("/change-status")
    @Operation(summary = "变更产品状态")
    public CommonResult<Boolean> changeProductStatus(@Valid @RequestBody ProductStatusActionReqVO reqVO) {
        productService.changeProductStatus(reqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除产品")
    public CommonResult<Boolean> deleteProduct(@Valid @RequestBody ProductDeleteReqVO reqVO) {
        productService.deleteProduct(reqVO);
        return success(true);
    }

}
