package org.dromara.main.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.CsSessionBo;
import org.dromara.main.domain.vo.CsSessionVo;
import org.dromara.main.service.ICsSessionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/main/session")
public class CsSessionController extends BaseController {

    private final ICsSessionService sessionService;

    /**
     * 创建或获取会话
     */
    @SaIgnore
    @PostMapping("/create")
    public R<CsSessionVo> createOrGetSession(@RequestBody Map<String, Object> params) {
        Integer sessionType = Integer.valueOf(params.get("sessionType").toString());
        Long fromUserId = Long.valueOf(params.get("fromUserId").toString());
        String fromUserName = (String) params.get("fromUserName");
        String fromUserAvatar = (String) params.get("fromUserAvatar");

        CsSessionVo session = sessionService.createOrGetSession(
            sessionType, fromUserId, fromUserName, fromUserAvatar
        );
        return R.ok(session);
    }

    /**
     * 获取会话列表
     */
    @GetMapping("/list")
    public TableDataInfo<CsSessionVo> list(CsSessionBo bo, PageQuery pageQuery) {
        return sessionService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取会话详情
     */
    @GetMapping("/{sessionId}")
    public R<CsSessionVo> getInfo(@PathVariable Long sessionId) {
        return R.ok(sessionService.queryById(sessionId));
    }

    /**
     * 结束会话
     */
    @Log(title = "结束会话", businessType = BusinessType.UPDATE)
    @PutMapping("/{sessionId}/end")
    public R<Void> endSession(@PathVariable Long sessionId) {
        return toAjax(sessionService.endSession(sessionId));
    }
}
