package com.haha.miniapp.controller;

import com.haha.common.vo.Result;
import com.haha.entity.Announcement;
import com.haha.service.AnnouncementService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序公告接口
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/announcement")
public class AppAnnouncementController {

    private final AnnouncementService announcementService;

    /**
     * 获取公告列表
     *
     * @param limit 数量限制，默认10条
     * @return 公告列表
     */
    @GetMapping("/list")
    public Result<List<Announcement>> getList(@RequestParam(defaultValue = "10") int limit) {
        try {
            List<Announcement> list = announcementService.getAppList(limit);
            return Result.success("success", list);
        } catch (Exception e) {
            log.error("获取公告列表失败", e);
            return Result.error("获取公告列表失败");
        }
    }

    /**
     * 获取公告详情
     *
     * @param id 公告ID
     * @return 公告详情
     */
    @GetMapping("/detail")
    public Result<Announcement> getDetail(@RequestParam Long id) {
        try {
            Announcement announcement = announcementService.getDetail(id);
            if (announcement == null) {
                return Result.error(404, "公告不存在");
            }
            return Result.success("success", announcement);
        } catch (Exception e) {
            log.error("获取公告详情失败", e);
            return Result.error("获取公告详情失败");
        }
    }
}
