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.PageResult;
import com.test.musichouduan.dto.Result;
import com.test.musichouduan.dto.SingerDTO;
import com.test.musichouduan.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 歌手控制器
 */
@RestController
@RequestMapping("/api/singer")
public class SingerController {

    private static final String HOT_SINGER_CACHE_KEY = "singer:hot_list";

    @Autowired
    private SingerService singerService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据ID获取歌手
     *
     * @param id 歌手ID
     * @return 响应结果
     */
    @SaIgnore
    @GetMapping("/{id}")
    public Result<SingerDTO> getSingerById(@PathVariable Long id) {
        SingerDTO singerDTO = singerService.getSingerById(id);
        return Result.success(singerDTO);
    }

    /**
     * 分页获取歌手列表
     *
     * @param page 页码
     * @param size 每页大小
     * @param keyword 关键字
     * @param typeId 类型ID
     * @return 响应结果
     */
    @SaIgnore
    @GetMapping("/list")
    public Result<PageResult<SingerDTO>> getSingerList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long typeId) {
        PageResult<SingerDTO> pageResult = singerService.getSingerList(page, size, keyword, typeId);
        return Result.success(pageResult);
    }

    /**
     * 搜索歌手
     *
     * @param keyword 关键字
     * @param page 页码
     * @param size 每页大小
     * @return 响应结果
     */
    @SaIgnore
    @GetMapping("/search")
    public Result<PageResult<SingerDTO>> searchSinger(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<SingerDTO> pageResult = singerService.getSingerList(page, size, keyword, null);
        return Result.success(pageResult);
    }

    /**
     * 获取最新歌手列表
     *
     * @param limit 限制数量
     * @return 响应结果
     */
    @SaIgnore
    @GetMapping("/latest")
    public Result<List<SingerDTO>> getLatestSinger(@RequestParam(defaultValue = "10") Integer limit) {
        List<SingerDTO> singerList = singerService.getLatestSinger(limit);
        return Result.success(singerList);
    }

    /**
     * 获取热门歌手列表
     *
     * @param limit 限制数量
     * @return 响应结果
     */
    @SaIgnore
    @GetMapping("/hot-list")
    public Result<List<SingerDTO>> getHotSinger(@RequestParam(defaultValue = "10") Integer limit) {
        List<SingerDTO> singerList = singerService.getHotSinger(limit);
        return Result.success(singerList);
    }

    /**
     * 添加歌手
     *
     * @param singerDTO 歌手DTO
     * @param avatar 头像文件
     * @return 响应结果
     */
    @SaIgnore
    @SaCheckRole("admin")
    @PostMapping("/add")
    public Result<SingerDTO> addSinger(
            @RequestPart("singer") SingerDTO singerDTO,
            @RequestPart(value = "avatar", required = false) MultipartFile avatar) {
        SingerDTO result = singerService.addSinger(singerDTO, avatar);
        redisTemplate.delete(HOT_SINGER_CACHE_KEY);
        singerService.getHotSinger(50);
        return Result.success("添加成功", result);
    }

    /**
     * 更新歌手
     *
     * @param id 歌手ID
     * @param singerDTO 歌手DTO
     * @param avatar 头像文件
     * @return 响应结果
     */

    @SaCheckRole("admin")
    @PutMapping("/{id}")
    public Result<SingerDTO> updateSinger(
            @PathVariable Long id,
            @RequestPart("singer") SingerDTO singerDTO,
            @RequestPart(value = "avatar", required = false) MultipartFile avatar) {
        SingerDTO result = singerService.updateSinger(id, singerDTO, avatar);
        redisTemplate.delete(HOT_SINGER_CACHE_KEY);
        singerService.getHotSinger(50);
        return Result.success("更新成功", result);
    }

    /**
     * 删除歌手
     *
     * @param id 歌手ID
     * @return 响应结果
     */
    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteSinger(@PathVariable Long id) {
        boolean result = singerService.deleteSinger(id);
        redisTemplate.delete(HOT_SINGER_CACHE_KEY);
        singerService.getHotSinger(50);
        return Result.success("删除成功", result);
    }

    /**
     * 收藏歌手
     *
     * @param id 歌手ID
     * @return 响应结果
     */
    @SaCheckLogin
    @PostMapping("/{id}/collect")
    public Result<Boolean> collectSinger(@PathVariable Long id) {
        boolean result = singerService.collectSinger(id);
        return Result.success("收藏成功", result);
    }

    /**
     * 取消收藏歌手
     *
     * @param id 歌手ID
     * @return 响应结果
     */
    @SaCheckLogin
    @DeleteMapping("/{id}/collect")
    public Result<Boolean> uncollectSinger(@PathVariable Long id) {
        boolean result = singerService.uncollectSinger(id);
        return Result.success("取消收藏成功", result);
    }

    /**
     * 检查当前用户是否收藏了该歌手
     *
     * @param id 歌手ID
     * @return 响应结果
     */
    @SaCheckLogin
    @GetMapping("/{id}/is-collected")
    public Result<Boolean> isCollected(@PathVariable Long id) {
        boolean result = singerService.isCollected(id);
        return Result.success(result);
    }

    /**
     * 获取用户收藏的歌手列表
     *
     * @param page 页码
     * @param size 每页大小
     * @return 响应结果
     */
    @SaCheckLogin
    @GetMapping("/collect")
    public Result<PageResult<SingerDTO>> getCollectedSinger(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<SingerDTO> pageResult = singerService.getCollectedSinger(page, size);
        return Result.success(pageResult);
    }

    /**
     * 添加音乐到歌手
     * @param singerId 歌手ID
     * @param musicId 音乐ID
     * @return 响应结果
     */
    @SaCheckRole("admin")
    @PostMapping("/{singerId}/music/{musicId}")
    public Result<Boolean> addMusicToSinger(
            @PathVariable Long singerId,
            @PathVariable Long musicId) {
        boolean result = singerService.addMusicToSinger(singerId, musicId);
        return Result.success("添加成功", result);
    }

    /**
     * 从歌手移除音乐
     * @param singerId 歌手ID
     * @param musicId 音乐ID
     * @return 响应结果
     */
    @SaCheckRole("admin")
    @DeleteMapping("/{singerId}/music/{musicId}")
    public Result<Boolean> removeMusicFromSinger(
            @PathVariable Long singerId,
            @PathVariable Long musicId) {
        boolean result = singerService.removeMusicFromSinger(singerId, musicId);
        return Result.success("移除成功", result);
    }
}
