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 request, Authentication authentication) {
        return playlistService.createPlaylist(request.name(), authentication.getName());
    }

    @PostMapping("/{id}/songs")
    @ResponseStatus(HttpStatus.CREATED)
    public PlaylistDTO addSongToPlaylist(@PathVariable Long id, @RequestBody SongCreateDTO song, Authentication authentication) {
        return playlistService.addSongToPlaylist(id, song, authentication.getName());
    }

    @DeleteMapping("/{playlistId}/songs/{songId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeSongFromPlaylist(@PathVariable Long playlistId, @PathVariable Long songId, Authentication authentication) {
        playlistService.removeSongFromPlaylist(playlistId, songId, authentication.getName());
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deletePlaylist(@PathVariable Long id, Authentication authentication) {
        playlistService.deletePlaylist(id, authentication.getName());
    }

    @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) {
        return playlistService.copyPlaylist(id, newName, authentication.getName());
    }
}
