package com.njcn.rdms.module.project.service.product;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.product.vo.activity.ProductActivityPageReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.activity.ProductActivityRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.activity.ProductActivityTimelinePageReqVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.activity.ProductActivityTimelineRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.setting.ProductSettingBaseInfoRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.setting.ProductSettingLifecycleRespVO;
import com.njcn.rdms.module.project.controller.admin.product.vo.setting.ProductSettingRespVO;
import com.njcn.rdms.module.project.dal.dataobject.product.ProductDO;
import com.njcn.rdms.module.project.dal.mysql.product.ProductMapper;
import com.njcn.rdms.module.project.constant.ProductObjectConstants;
import com.njcn.rdms.module.project.enums.ErrorCodeConstants;
import com.njcn.rdms.module.project.framework.security.annotation.CheckObjectPermission;
import com.njcn.rdms.module.system.api.user.AdminUserApi;
import com.njcn.rdms.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
public class ProductSettingServiceImpl implements ProductSettingService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private ProductStatusViewService productStatusViewService;
    @Resource
    private ProductActivityQueryService productActivityQueryService;
    @Resource
    private ProductActivityTimelineQueryService productActivityTimelineQueryService;

    @Override
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId", accessible = true)
    public ProductSettingRespVO getProductSettings(Long productId) {
        ProductDO product = validateProductExists(productId);
        ProductSettingRespVO respVO = new ProductSettingRespVO();
        respVO.setBaseInfo(buildBaseInfo(product));
        respVO.setLifecycle(buildLifecycle(product));
        return respVO;
    }

    @Override
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId", accessible = true)
    public PageResult<ProductActivityRespVO> getProductActivities(Long productId, ProductActivityPageReqVO reqVO) {
        validateProductExists(productId);
        return productActivityQueryService.getProductActivities(productId, reqVO);
    }

    @Override
    @CheckObjectPermission(objectType = ProductObjectConstants.OBJECT_TYPE, objectId = "#productId", accessible = true)
    public PageResult<ProductActivityTimelineRespVO> getProductActivityTimelinePage(
            Long productId, ProductActivityTimelinePageReqVO reqVO) {
        validateProductExists(productId);
        return productActivityTimelineQueryService.getProductActivityTimelinePage(productId, reqVO);
    }

    private ProductDO validateProductExists(Long productId) {
        if (productId == null) {
            throw exception(ErrorCodeConstants.PRODUCT_NOT_EXISTS);
        }
        ProductDO product = productMapper.selectById(productId);
        if (product == null) {
            throw exception(ErrorCodeConstants.PRODUCT_NOT_EXISTS);
        }
        return product;
    }

    private ProductSettingBaseInfoRespVO buildBaseInfo(ProductDO product) {
        ProductSettingBaseInfoRespVO baseInfo = new ProductSettingBaseInfoRespVO();
        baseInfo.setId(product.getId());
        baseInfo.setCode(product.getCode());
        baseInfo.setDirectionCode(product.getDirectionCode());
        baseInfo.setName(product.getName());
        baseInfo.setManagerUserId(product.getManagerUserId());
        baseInfo.setManagerUserNickname(getManagerNickname(product.getManagerUserId()));
        baseInfo.setDescription(product.getDescription());
        baseInfo.setStatusCode(product.getStatusCode());
        baseInfo.setLastStatusReason(product.getLastStatusReason());
        return baseInfo;
    }

    private ProductSettingLifecycleRespVO buildLifecycle(ProductDO product) {
        return productStatusViewService.getLifecycle(product.getStatusCode(), product.getLastStatusReason());
    }

    private String getManagerNickname(Long managerUserId) {
        if (managerUserId == null) {
            return null;
        }
        CommonResult<AdminUserRespDTO> result = adminUserApi.getUser(managerUserId);
        AdminUserRespDTO user = result == null ? null : result.getCheckedData();
        return user == null ? null : user.getNickname();
    }

}
