package com.chinaweal.aiccs.org.controller;

import com.chinaweal.aiccs.org.entity.dto.ImsOrgDTO;
import com.chinaweal.aiccs.org.entity.dto.ImsUserDTO;
import com.chinaweal.aiccs.org.entity.vo.ImsResultVO;
import com.chinaweal.aiccs.org.service.IImsOrgService;
import com.chinaweal.aiccs.org.service.IImsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;

/**
 * IMS统一身份管理系统数据同步接口
 * 用于接收吉大正元IMS推送的机构和用户数据
 *
 * @author aiccs
 * @since 2026-03-23
 */
@Slf4j
@RestController
@RequestMapping("/ims/v1")
@Api(tags = "IMS统一身份同步接口")
public class ImsSyncController {

    @Autowired
    private IImsOrgService imsOrgService;

    @Autowired
    private IImsUserService imsUserService;

    /**
     * 机构同步接口
     * 接收IMS推送的机构数据
     *
     * @param dto 机构同步数据
     * @return 同步结果
     */
    @PostMapping("/org/orgSynchronous")
    @ApiOperation(value = "机构同步接口", notes = "接收IMS推送的机构数据，支持新增、修改、删除操作")
    public ImsResultVO orgSynchronous(@RequestBody ImsOrgDTO dto, HttpServletRequest request) {
        log.info("IMS机构同步请求: type={}, orgCode={}", dto.getType(), dto.getCode());
        try {
            boolean result = imsOrgService.syncOrg(dto);
            if (result) {
                return ImsResultVO.ok();
            } else {
                return ImsResultVO.fail("机构同步失败");
            }
        } catch (Exception e) {
            log.error("IMS机构同步异常: {}", e.getMessage(), e);
            return ImsResultVO.fail("机构同步异常: " + e.getMessage());
        }
    }

    /**
     * 用户同步接口
     * 接收IMS推送的用户数据
     *
     * @param dto 用户同步数据
     * @return 同步结果
     */
    @PostMapping("/user/userSynchronous")
    @ApiOperation(value = "用户同步接口", notes = "接收IMS推送的用户数据，支持新增、修改、删除操作")
    public ImsResultVO userSynchronous(@RequestBody ImsUserDTO dto, HttpServletRequest request) {
        log.info("IMS用户同步请求: type={}, userCode={}", dto.getType(), dto.getUsercode());

        try {
            boolean result = imsUserService.syncUser(dto);
            if (result) {
                return ImsResultVO.ok();
            } else {
                return ImsResultVO.fail("用户同步失败");
            }
        } catch (Exception e) {
            log.error("IMS用户同步异常: {}", e.getMessage(), e);
            return ImsResultVO.fail("用户同步异常: " + e.getMessage());
        }
    }

    /**
     * 连接测试接口
     * 用于IMS系统测试连接是否正常
     *
     * @return 测试结果
     */
    @PostMapping("/test")
    @ApiOperation(value = "连接测试接口", notes = "测试与IMS系统的连接是否正常")
    public ImsResultVO testConnection() {
        log.info("IMS连接测试请求");
        return ImsResultVO.ok();
    }
}
