package com.vibevault.controller;

import com.vibevault.dto.PlaylistCreateDTO;
import com.vibevault.dto.PlaylistDTO;
import com.vibevault.dto.SongCreateDTO;
import com.vibevault.service.PlaylistService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 歌单 REST 控制器
 * 
 * 需要实现以下端点：
 * - GET /api/playlists - 获取所有歌单（公开）
 * - GET /api/playlists/{id} - 获取指定歌单（公开）
 * - POST /api/playlists - 创建歌单（需认证）
 * - POST /api/playlists/{id}/songs - 添加歌曲（需认证）
 * - DELETE /api/playlists/{playlistId}/songs/{songId} - 移除歌曲（需认证）
 * - DELETE /api/playlists/{id} - 删除歌单（需认证）
 * 
 * [Advanced] 额外端点：
 * - GET /api/playlists/search?keyword=xxx - 搜索歌单
 * - POST /api/playlists/{id}/copy?newName=xxx - 复制歌单
 * 
 * 提示：
 * - 使用 Authentication 参数获取当前用户名：authentication.getName()
 * - 使用 @ResponseStatus 设置正确的 HTTP 状态码
 */
@RestController
@RequestMapping("/api/playlists")
public class PlaylistController {

    private final PlaylistService playlistService;

    public PlaylistController(PlaylistService playlistService) {
        this.playlistService = playlistService;
    }

    /**
     * 获取所有歌单（公开）
     */
    @GetMapping
    public List<PlaylistDTO> getAllPlaylists() {
        return playlistService.getAllPlaylists();
    }

    /**
     * 获取指定歌单（公开）
     */
    @GetMapping("/{id}")
    public PlaylistDTO getPlaylistById(@PathVariable Long id) {
        return playlistService.getPlaylistById(id);
    }

    /**
     * 创建歌单（需认证）
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PlaylistDTO createPlaylist(@RequestBody PlaylistCreateDTO playlistCreateDTO, Authentication authentication) {
        String username = authentication.getName();
        return playlistService.createPlaylist(playlistCreateDTO.name(), username);
    }

    /**
     * 添加歌曲到歌单（需认证）
     */
    @PostMapping("/{id}/songs")
    @ResponseStatus(HttpStatus.CREATED)
    public PlaylistDTO addSongToPlaylist(@PathVariable Long id, @RequestBody SongCreateDTO songCreateDTO, Authentication authentication) {
        String username = authentication.getName();
        return playlistService.addSongToPlaylist(id, songCreateDTO, username);
    }

    /**
     * 从歌单移除歌曲（需认证）
     */
    @DeleteMapping("/{playlistId}/songs/{songId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeSongFromPlaylist(@PathVariable Long playlistId, @PathVariable Long songId, Authentication authentication) {
        String username = authentication.getName();
        playlistService.removeSongFromPlaylist(playlistId, songId, username);
    }

    /**
     * 删除歌单（需认证）
     */
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deletePlaylist(@PathVariable Long id, Authentication authentication) {
        String username = authentication.getName();
        playlistService.deletePlaylist(id, username);
    }

    /**
     * 搜索歌单
     */
    @GetMapping("/search")
    public List<PlaylistDTO> searchPlaylists(@RequestParam String keyword) {
        return playlistService.searchPlaylists(keyword);
    }

    /**
     * 复制歌单
     */
    @PostMapping("/{id}/copy")
    @ResponseStatus(HttpStatus.CREATED)
    public PlaylistDTO copyPlaylist(@PathVariable Long id, @RequestParam String newName, Authentication authentication) {
        String username = authentication.getName();
        return playlistService.copyPlaylist(id, newName, username);
    }
}
