package com.snack.server.module.notice.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.notice.dto.req.NoticePageReq;
import com.snack.server.module.notice.dto.req.NoticeSaveReq;
import com.snack.server.module.notice.service.NoticeService;
import com.snack.server.module.notice.vo.NoticeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 公告管理（管理端）
 */
@Tag(name = "07. 系统公告")
@RestController
@RequestMapping("/api/admin/notice")
@RequiredArgsConstructor
public class NoticeAdminController {

    private final NoticeService noticeService;

    @Operation(summary = "分页查询公告")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<NoticeVO>> page(NoticePageReq req) {
        return Result.ok(noticeService.pageNotices(req));
    }

    @Operation(summary = "获取公告详情")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<NoticeVO> detail(@Parameter(description = "公告 ID") @PathVariable Long id) {
        return Result.ok(noticeService.getNoticeById(id));
    }

    @Operation(summary = "创建公告")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody NoticeSaveReq req) {
        Long publisherId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null;
        return Result.ok(noticeService.createNotice(req, publisherId));
    }

    @Operation(summary = "更新公告")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody NoticeSaveReq req) {
        noticeService.updateNotice(req);
        return Result.ok();
    }

    @Operation(summary = "删除公告")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "公告 ID") @PathVariable Long id) {
        noticeService.deleteNotice(id);
        return Result.ok();
    }

    @Operation(summary = "上线 / 下线")
    @SaCheckLogin
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateStatus(
            @Parameter(description = "公告 ID") @PathVariable Long id,
            @Parameter(description = "状态：0-下线 1-上线") @PathVariable Integer status) {
        noticeService.updateStatus(id, status);
        return Result.ok();
    }

    @Operation(summary = "置顶 / 取消置顶")
    @SaCheckLogin
    @PutMapping("/{id}/top/{isTop}")
    public Result<Void> updateTop(
            @Parameter(description = "公告 ID") @PathVariable Long id,
            @Parameter(description = "是否置顶：0-否 1-是") @PathVariable Integer isTop) {
        noticeService.updateTop(id, isTop);
        return Result.ok();
    }
}
