package com.ruoyi.xq.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.PageModel;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.dto.app.common.IdReq;
import com.ruoyi.xq.dto.app.dynamic.*;
import com.ruoyi.xq.service.DynamicService;
import com.ruoyi.xq.service.DynamicStarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/dynamic")
@Tag(name = "动态相关接口")
public class DynamicAppController {
    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private DynamicStarService dynamicStarService;

    @PostMapping("/add")
    @Operation(summary = "新增动态")
    @Log(title = "新增动态", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> addDynamic(@RequestBody AddDynamicReq req){
        req.setUserId(LoginHelper.getUserId());
        dynamicService.push(req);
        return R.ok();
    }

    @PostMapping("/star")
    @Operation(summary = "点赞动态")
    @Log(title = "点赞动态", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> starDynamic(@RequestBody DynamicStarReq req){
        Long userId = LoginHelper.getUserId();
        if(req.getStar() == null){
            return R.fail();
        }
        if(req.getStar() == 1){
            dynamicStarService.star(req.getDynamicId(),userId);
            return R.ok();
        }
        if(req.getStar() == 2){
            dynamicStarService.unStar(req.getDynamicId(),userId);
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/page")
    @Operation(summary = "动态列表-分页")
    @Log(title = "动态列表-分页", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<PageModel<DynamicListVo>> page(PageQuery pageQuery, DynamicQuery dynamicQuery){
        dynamicQuery.initBirthdayByAge();
        Page<DynamicListVo> page = dynamicService.pageApp(pageQuery, dynamicQuery);
        return R.ok(PageModel.build(page));
    }

    @PostMapping("/delete")
    @Operation(summary = "删除我的动态")
    @Log(title = "删除我的动态", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> delete(@RequestBody IdReq idReq){
        dynamicService.removeDynamicOfApp(idReq.getId());
        return R.ok();
    }

    @GetMapping("/current/page")
    @Operation(summary = "我发布的动态列表-分页")
    @Log(title = "我发布的动态列表-分页", businessType = BusinessType.OTHER,isPrintResponseData = false, isSaveDb = false)
    public R<PageModel<MyDynamicListVo>> currentPage(PageQuery pageQuery){
        Page<MyDynamicListVo> page = dynamicService.currentPage(pageQuery);
        return R.ok(PageModel.build(page));
    }

    @GetMapping("/myStarDynamic")
    @Operation(summary = "我收到的动态点赞-分页")
    @Log(title = "我收到的动态点赞-分页", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<PageModel<MyStarDynamicListVO>> myStarDynamic(PageQuery pageQuery, MyStarDynamicQuery dynamicQuery){
        Page<MyStarDynamicListVO> page = dynamicStarService.myStarDynamic(pageQuery, dynamicQuery);
        return R.ok(PageModel.build(page));
    }
}
