
package com.tmerclub.cloud.live.controller.multishop;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.live.dto.LiveRoomDTO;
import com.tmerclub.cloud.live.service.LiveRoomProdService;
import com.tmerclub.cloud.live.service.LiveRoomService;
import com.tmerclub.cloud.live.vo.LiveRoomProdVO;
import com.tmerclub.cloud.live.vo.LiveRoomVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 直播间
 * @author TRACK
 */
@RestController("multishopLiveRoomController")
@RequestMapping("/m/live_room")
@Tag(name = "商家直播间")
public class LiveRoomController {

    @Autowired
    private LiveRoomService liveRoomService;
    @Autowired
    private LiveRoomProdService liveRoomProdService;

    @GetMapping("/page")
    @Operation(summary = "获取直播间列表", description = "分页获取直播间列表")
    public ServerResponseEntity<PageVO<LiveRoomVO>> page(@Valid PageDTO pageDTO, LiveRoomDTO liveRoom) {
        liveRoom.setShopId(AuthUserContext.get().getTenantId());
        PageVO<LiveRoomVO> liveRoomPage = liveRoomService.page(pageDTO, liveRoom);
        return ServerResponseEntity.success(liveRoomPage);
    }

    @GetMapping
    @Operation(summary = "获取直播间", description = "根据roomId获取直播间")
    public ServerResponseEntity<LiveRoomVO> getByRoomId(@RequestParam Long roomId) {
        LiveRoomVO liveRoomVO = liveRoomService.getByRoomId(roomId);
        if (StrUtil.isNotBlank(liveRoomVO.getUserMobile())) {
            liveRoomVO.setUserMobile(PhoneUtil.hideBetween(liveRoomVO.getUserMobile()).toString());
        }
        return ServerResponseEntity.success(liveRoomVO);
    }

    @PostMapping
    @Operation(summary = "保存直播间", description = "保存直播间")
    public ServerResponseEntity<Void> save(@Valid @RequestBody LiveRoomDTO liveRoomDTO) {
        liveRoomDTO.setShopId(AuthUserContext.get().getTenantId());
        liveRoomService.save(liveRoomDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新直播间", description = "更新直播间")
    public ServerResponseEntity<Void> update(@Valid @RequestBody LiveRoomDTO liveRoomDTO) {
        liveRoomDTO.setShopId(AuthUserContext.get().getTenantId());
        liveRoomService.update(liveRoomDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除直播间", description = "根据直播间id删除直播间")
    public ServerResponseEntity<Void> delete(@RequestParam Long roomId) {
        liveRoomService.deleteById(roomId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_live_room_prod")
    @Operation(summary = "获取直播间的直播商品列表")
    public ServerResponseEntity<List<LiveRoomProdVO>> listLiveRoomProd(@RequestParam Long roomId) {
        return ServerResponseEntity.success(liveRoomProdService.listLiveRoomProdInfo(roomId));
    }
}
