package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.storeplatform.entity.StorePlatformDiscussion;
import shop.alien.storeplatform.service.StorePlatformDiscussionService;
import shop.alien.storeplatform.vo.StorePlatformDiscussionReplyVo;
import shop.alien.storeplatform.vo.StorePlatformDiscussionUserVo;

import java.util.List;

/**
 * 商户平台-店铺讨论Controller
 *
 * @author alien
 * @since 2025-12-30
 */
@Slf4j
@Api(tags = {"商户平台-店铺问答讨论"})
@RestController
@RequestMapping("/platformStoreDiscussion")
@RequiredArgsConstructor
public class StorePlatformDiscussionController {

    private final StorePlatformDiscussionService storePlatformDiscussionService;

    @ApiOperation("发布一级问答讨论 (主贴)")
    @PostMapping("/postTopic")
    public R<Boolean> postTopic(@RequestBody StorePlatformDiscussion discussion) {
        log.info("StorePlatformDiscussionController.postTopic?discussion={}", discussion);
        try {
            boolean success = storePlatformDiscussionService.postTopic(discussion);
            return success ? R.success("发布成功") : R.fail("发布失败");
        } catch (Exception e) {
            log.error("发布讨论失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("回复问答讨论")
    @PostMapping("/postReply")
    public R<Boolean> postReply(@RequestBody StorePlatformDiscussion discussion) {
        log.info("StorePlatformDiscussionController.postReply?discussion={}", discussion);
        try {
            boolean success = storePlatformDiscussionService.postReply(discussion);
            return success ? R.success("回复成功") : R.fail("回复失败");
        } catch (Exception e) {
            log.error("回复讨论失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("点赞讨论")
    @PostMapping("/like/{id}")
    public R<Boolean> like(@PathVariable Integer id) {
        log.info("StorePlatformDiscussionController.like?id={}", id);
        boolean success = storePlatformDiscussionService.likeDiscussion(id);
        return success ? R.success("点赞成功") : R.fail("点赞失败");
    }

    @ApiOperation("分页获取店铺一级问答讨论列表 (支持搜索和排序)")
    @GetMapping("/pageRoot")
    public R<IPage<StorePlatformDiscussionUserVo>> pageRoot(
            @ApiParam(value = "店铺ID", required = true) @RequestParam Integer storeId,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "搜索标签 (AI 匹配内容)") @RequestParam(required = false) String tags,
            @ApiParam(value = "排序方式: 1-相关度降序, 2-点赞数降序, 3-时间升序, 4-时间降序", defaultValue = "4") @RequestParam(defaultValue = "4") Integer sortMode) {
        log.info("StorePlatformDiscussionController.pageRoot?storeId={}, pageNum={}, pageSize={}, tags={}, sortMode={}", storeId, pageNum, pageSize, tags, sortMode);
        return R.data(storePlatformDiscussionService.pageRootDiscussions(storeId, pageNum, pageSize, tags, sortMode));
    }

    @ApiOperation("分页获取某条一级问答讨论下的所有回复 (支持搜索和排序)")
    @GetMapping("/pageReplies")
    public R<StorePlatformDiscussionReplyVo> pageReplies(
            @ApiParam(value = "一级讨论ID (rootId)", required = true) @RequestParam Integer rootId,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "搜索标签 (AI 匹配内容)") @RequestParam(required = false) String tags,
            @ApiParam(value = "排序方式: 1-相关度降序, 2-点赞数降序, 3-时间升序, 4-时间降序", defaultValue = "3") @RequestParam(defaultValue = "3") Integer sortMode) {
        log.info("StorePlatformDiscussionController.pageReplies?rootId={}, pageNum={}, pageSize={}, tags={}, sortMode={}", rootId, pageNum, pageSize, tags, sortMode);
        return R.data(storePlatformDiscussionService.pageRepliesByRootId(rootId, pageNum, pageSize, tags, sortMode));
    }

    @ApiOperation("获取店铺问答讨论列表 (支持搜索和排序)")
    @GetMapping("/list")
    public R<List<StorePlatformDiscussionUserVo>> list(
            @ApiParam(value = "店铺ID", required = true) @RequestParam Integer storeId,
            @ApiParam(value = "搜索标签 (AI 匹配内容)") @RequestParam(required = false) String tags,
            @ApiParam(value = "排序方式: 1-相关度降序, 2-点赞数降序, 3-时间升序, 4-时间降序", defaultValue = "4") @RequestParam(defaultValue = "4") Integer sortMode) {
        log.info("StorePlatformDiscussionController.list?storeId={}, tags={}, sortMode={}", storeId, tags, sortMode);
        return R.data(storePlatformDiscussionService.listByStoreId(storeId, tags, sortMode));
    }

    @ApiOperation("撤消问答讨论")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        log.info("StorePlatformDiscussionController.delete?id={}", id);
        boolean success = storePlatformDiscussionService.deleteDiscussion(id);
        return success ? R.success("撤消成功") : R.fail("撤消失败");
    }
}
