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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.banner.dto.req.BannerPageReq;
import com.snack.server.module.banner.dto.req.BannerSaveReq;
import com.snack.server.module.banner.service.BannerService;
import com.snack.server.module.banner.vo.BannerVO;
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 = "轮播图管理")
@RestController
@RequestMapping("/api/admin/banner")
@RequiredArgsConstructor
public class BannerAdminController {

    private final BannerService bannerService;

    @Operation(summary = "分页查询轮播图")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<BannerVO>> page(BannerPageReq req) {
        return Result.ok(bannerService.pageBanners(req));
    }

    @Operation(summary = "轮播图详情")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<BannerVO> detail(@Parameter(description = "轮播图 ID") @PathVariable Long id) {
        return Result.ok(bannerService.getById(id));
    }

    @Operation(summary = "创建轮播图")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody BannerSaveReq req) {
        return Result.ok(bannerService.create(req));
    }

    @Operation(summary = "更新轮播图")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody BannerSaveReq req) {
        bannerService.update(req);
        return Result.ok();
    }

    @Operation(summary = "删除轮播图")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "轮播图 ID") @PathVariable Long id) {
        bannerService.delete(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) {
        bannerService.updateStatus(id, status);
        return Result.ok();
    }

    @Operation(summary = "调整排序")
    @SaCheckLogin
    @PutMapping("/{id}/sort/{sort}")
    public Result<Void> updateSort(
            @Parameter(description = "轮播图 ID") @PathVariable Long id,
            @Parameter(description = "排序号") @PathVariable Integer sort) {
        bannerService.updateSort(id, sort);
        return Result.ok();
    }
}
