package com.test.musichouduan.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.test.musichouduan.dto.BannerDTO;
import com.test.musichouduan.dto.PageResult;
import com.test.musichouduan.dto.Result;
import com.test.musichouduan.service.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 轮播图控制器
 */
@Tag(name = "轮播图接口", description = "轮播图的增删改查操作")
@RestController
@RequestMapping("/api/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 根据ID获取轮播图
     *
     * @param id 轮播图ID
     * @return 响应结果
     */
    @Operation(summary = "根据ID获取轮播图", description = "根据轮播图ID获取轮播图详情")
    @GetMapping("/{id}")
    public Result<BannerDTO> getBannerById(@Parameter(description = "轮播图ID") @PathVariable Long id) {
        BannerDTO bannerDTO = bannerService.getBannerById(id);
        return Result.success(bannerDTO);
    }

    /**
     * 获取轮播图列表
     *
     * @return 响应结果
     */
    @SaIgnore
    @Operation(summary = "获取轮播图列表", description = "获取所有启用状态的轮播图列表")
    @GetMapping("/list")
    public Result<List<BannerDTO>> getBannerList() {
        List<BannerDTO> bannerList = bannerService.getBannerList();
        return Result.success(bannerList);
    }

    /**
     * 分页获取轮播图列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 响应结果
     */
    @Operation(summary = "分页获取轮播图列表", description = "分页获取轮播图列表，需要管理员权限")
    @SaCheckRole("admin")
    @GetMapping("/page")
    public Result<PageResult<BannerDTO>> getBannerPage(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        PageResult<BannerDTO> pageResult = bannerService.getBannerPage(page, size);
        return Result.success(pageResult);
    }

    /**
     * 添加轮播图
     *
     * @param bannerDTO 轮播图DTO
     * @param image 图片文件
     * @return 响应结果
     */
    @Operation(summary = "添加轮播图", description = "添加轮播图，需要管理员权限")
    @SaCheckRole("admin")
    @PostMapping("/add")
    public Result<BannerDTO> addBanner(
            @Parameter(description = "轮播图信息") @RequestPart("banner") BannerDTO bannerDTO,
            @Parameter(description = "轮播图图片文件") @RequestPart("image") MultipartFile image) {
        BannerDTO result = bannerService.addBanner(bannerDTO, image);
        return Result.success("添加成功", result);
    }

    /**
     * 更新轮播图
     *
     * @param id 轮播图ID
     * @param bannerDTO 轮播图DTO
     * @param image 图片文件
     * @return 响应结果
     */
    @Operation(summary = "更新轮播图", description = "更新轮播图信息，需要管理员权限")
    @SaCheckRole("admin")
    @PutMapping("/{id}")
    public Result<BannerDTO> updateBanner(
            @Parameter(description = "轮播图ID") @PathVariable Long id,
            @Parameter(description = "轮播图信息") @RequestPart("banner") BannerDTO bannerDTO,
            @Parameter(description = "轮播图图片文件，可选") @RequestPart(value = "image", required = false) MultipartFile image) {
        BannerDTO result = bannerService.updateBanner(id, bannerDTO, image);
        return Result.success("更新成功", result);
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图ID
     * @return 响应结果
     */
    @Operation(summary = "删除轮播图", description = "删除轮播图，需要管理员权限")
    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteBanner(@Parameter(description = "轮播图ID") @PathVariable Long id) {
        boolean result = bannerService.deleteBanner(id);
        return Result.success("删除成功", result);
    }

    /**
     * 更新轮播图状态
     *
     * @param id 轮播图ID
     * @param status 状态
     * @return 响应结果
     */
    @Operation(summary = "更新轮播图状态", description = "更新轮播图状态，0-禁用，1-启用，需要管理员权限")
    @SaCheckRole("admin")
    @PutMapping("/{id}/status")
    public Result<Boolean> updateBannerStatus(
            @Parameter(description = "轮播图ID") @PathVariable Long id,
            @Parameter(description = "状态，0-禁用，1-启用", example = "1") @RequestParam Integer status) {
        boolean result = bannerService.updateBannerStatus(id, status);
        return Result.success("更新成功", result);
    }

    /**
     * 更新轮播图排序
     *
     * @param id 轮播图ID
     * @param orderNum 排序号
     * @return 响应结果
     */
    @Operation(summary = "更新轮播图排序", description = "更新轮播图排序号，数字越小越靠前，需要管理员权限")
    @SaCheckRole("admin")
    @PutMapping("/{id}/order")
    public Result<Boolean> updateBannerOrder(
            @Parameter(description = "轮播图ID") @PathVariable Long id,
            @Parameter(description = "排序号，数字越小越靠前", example = "1") @RequestParam Integer orderNum) {
        boolean result = bannerService.updateBannerOrder(id, orderNum);
        return Result.success("更新成功", result);
    }
}
