package com.lesingle.modules.leai.controller;

import com.lesingle.common.exception.BusinessException;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.leai.config.LeaiConfig;
import com.lesingle.modules.leai.service.LeaiApiClient;
import com.lesingle.modules.leai.vo.LeaiTokenVO;
import com.lesingle.modules.sys.entity.SysUser;
import com.lesingle.modules.sys.service.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 乐读派认证入口控制器
 * 前端 iframe 模式的主入口
 */
@Slf4j
@Tag(name = "乐读派认证")
@RestController
@RequestMapping("/leai-auth")
@RequiredArgsConstructor
public class LeaiAuthController {

    private final LeaiApiClient leaiApiClient;
    private final LeaiConfig leaiConfig;
    private final ISysUserService sysUserService;

    /**
     * 前端 iframe 主入口：返回 token 信息 JSON
     * 需要登录认证
     */
    @GetMapping("/token")
    @Operation(summary = "获取乐读派创作 Token")
    public Result<LeaiTokenVO> getToken() {
        Long userId = SecurityUtil.getCurrentUserId();
        SysUser user = sysUserService.getById(userId);
        if (user == null) {
            throw new BusinessException(404, "用户不存在");
        }

        String phone = user.getPhone();
        if (phone == null || phone.isEmpty()) {
            throw new BusinessException(400, "用户未绑定手机号，无法使用创作功能");
        }

        try {
            String token = leaiApiClient.exchangeToken(phone);

            // Entity → VO 转换（Controller 层负责）
            // 注意: orgId 对应本项目的租户 code（tenant_code）
            LeaiTokenVO vo = new LeaiTokenVO();
            vo.setToken(token);
            vo.setOrgId(leaiConfig.getOrgId()); // 即租户 tenant_code

            log.info("[乐读派] 获取创作Token成功, userId={}, phone={}", userId, phone);
            return Result.success(vo);

        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("[乐读派] 获取创作Token失败, userId={}", userId, e);
            throw new BusinessException(500, "获取创作Token失败: " + e.getMessage());
        }
    }

    /**
     * iframe 内 Token 刷新接口
     * 前端 JS 在收到 TOKEN_EXPIRED postMessage 时调用此接口
     */
    @GetMapping("/refresh-token")
    @Operation(summary = "刷新乐读派 Token")
    public Result<LeaiTokenVO> refreshToken() {
        Long userId = SecurityUtil.getCurrentUserId();
        SysUser user = sysUserService.getById(userId);
        if (user == null || user.getPhone() == null || user.getPhone().isEmpty()) {
            throw new BusinessException(401, "请先登录并绑定手机号");
        }

        String phone = user.getPhone();
        try {
            String token = leaiApiClient.exchangeToken(phone);

            // Entity → VO 转换（Controller 层负责）
            LeaiTokenVO vo = new LeaiTokenVO();
            vo.setToken(token);
            vo.setOrgId(leaiConfig.getOrgId());

            log.info("[乐读派] Token刷新成功, userId={}", userId);
            return Result.success(vo);

        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("[乐读派] Token刷新失败, userId={}", userId, e);
            throw new BusinessException(500, "Token刷新失败: " + e.getMessage());
        }
    }
}
