package com.yupi.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.project.annotation.AuthCheck;
import com.yupi.project.common.BaseResponse;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.constant.UserConstant;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.model.dto.charging_session.ChargingSessionQueryRequest;
import com.yupi.project.model.entity.ChargingSession;
import com.yupi.project.model.entity.User;
import com.yupi.project.model.vo.ChargingSessionVO;
import com.yupi.project.service.ChargingSessionService;
import com.yupi.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 充电会话管理接口 (管理员权限)
 */
@RestController
@RequestMapping("/admin/session")
@Slf4j
@Api(tags = "充电会话管理接口 (管理员)")
public class ChargingSessionAdminController {

    @Resource
    private ChargingSessionService chargingSessionService;

    @Resource
    private UserService userService; // 可能需要用于填充用户信息

    @ApiOperation("管理员分页获取所有充电会话列表")
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<ChargingSessionVO>> listAllChargingSessionsByPage(@RequestBody ChargingSessionQueryRequest queryRequest, HttpServletRequest request) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 管理员查询，不强制设置userId
        long current = queryRequest.getCurrent();
        long size = queryRequest.getPageSize();

        Page<ChargingSession> sessionPage = chargingSessionService.page(new Page<>(current, size),
                chargingSessionService.getQueryWrapper(queryRequest));

        Page<ChargingSessionVO> sessionVOPage = new Page<>(sessionPage.getCurrent(), sessionPage.getSize(), sessionPage.getTotal());
        
        // 转换VO并填充用户信息
        List<ChargingSessionVO> voList = sessionPage.getRecords().stream().map(session -> {
            ChargingSessionVO vo = ChargingSessionVO.objToVo(session);
            if (session.getUserId() != null) {
                User user = userService.getById(session.getUserId());
                if (user != null) {
                    vo.setUser(user); // UserVO 转换会在 setUser 方法内进行
                }
            }
            // TODO: 填充机器人和车位快照对应的实体信息 (如果VO中需要)
            return vo;
        }).collect(Collectors.toList());
        
        sessionVOPage.setRecords(voList);

        return ResultUtils.success(sessionVOPage);
    }
    
    // TODO: 管理员可能需要的其他接口，例如：
    // - 管理员手动取消某个会话 (不同于用户取消)
    // - 查看特定会话的详细日志或关联任务
    // - 重试某个失败的支付等

} 