package io.controller;


import io.annotation.Login;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.modules.item.dto.ArticleDTO;
import io.modules.item.service.ArticleService;
import io.modules.item.service.DictService;
import io.modules.item.service.UserBehaviorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章表
 */
@RestController
@RequestMapping("/api/article")
@Tag(name = "文章表")
@CrossOrigin
public class ArticleFrontController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserBehaviorService userBehaviorService;

    @Autowired
    private DictService dictService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "type", description = "类型", in = ParameterIn.QUERY, ref = "String")
    })
    public Result<PageData<ArticleDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<ArticleDTO> page = articleService.page(params);
        return new Result<PageData<ArticleDTO>>().ok(page);
    }
    @GetMapping("top")
    @Operation(summary = "推荐")
    public Result<List<ArticleDTO>> top() {
        List<ArticleDTO> res = new ArrayList<>();
        List<Long> topItems = userBehaviorService.getTopItems();
        // 根据ID获取文章数据
        for (Long id : topItems) {
            ArticleDTO articleDTO = articleService.get(id);  // 假设 get 方法返回 ArticleDTO
            if (articleDTO != null) {
                res.add(articleDTO);
            }
        }

        return new Result< List<ArticleDTO>>().ok(res);
    }


    @Login
    @GetMapping("list")
    @Operation(summary = "信息")
    public Result<List<ArticleDTO>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> params,
                                         @Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        params.put("userId",userId);
        List<ArticleDTO> list = articleService.list(params);
        return new Result< List<ArticleDTO>>().ok(list);
    }
    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<ArticleDTO> get(@PathVariable("id") Long id) {
        ArticleDTO data = articleService.get(id);
        return new Result<ArticleDTO>().ok(data);
    }

    @Login
    @PostMapping
    @Operation(summary = "保存")
    public Result save(@Parameter(hidden = true) @RequestAttribute("userId") Long userId, @RequestBody ArticleDTO dto) {
        //效验数据
        System.out.println("用户" + userId);
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setUserId(userId);

        articleService.save(dto);
        return new Result();
    }

    @Login
    @PutMapping
    @Operation(summary = "修改")
    public Result update(@Parameter(hidden = true) @RequestAttribute("userId") Long userId, @RequestBody ArticleDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUserId(userId);
        articleService.update(dto);
        return new Result();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    public Result delete(@PathVariable Long id) {
        //效验数据
        articleService.deleteById(id);
        return new Result();
    }
}
