package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.CourseLikeFavoriteDto;
import com.dora.service.CourseLikeFavoriteService;
import com.dora.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;



/**
 * 课程点赞和收藏控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/course")
@RequiredArgsConstructor
@Tag(name = "课程点赞收藏管理", description = "课程点赞和收藏相关接口")
public class CourseLikeFavoriteController {

    private final CourseLikeFavoriteService courseLikeFavoriteService;

    @PostMapping("/{courseId}/like")
    @Operation(summary = "点赞/取消点赞课程", description = "对课程进行点赞或取消点赞操作")
    public Result<CourseLikeFavoriteDto.LikeResponse> toggleLike(
            @Parameter(description = "课程ID", example = "1") @PathVariable Long courseId) {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            CourseLikeFavoriteDto.LikeResponse response = courseLikeFavoriteService.toggleLike(userId, courseId);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("课程点赞操作失败 - courseId: {}", courseId, e);
            return Result.error(500, "操作失败");
        }
    }

    @PostMapping("/{courseId}/favorite")
    @Operation(summary = "收藏/取消收藏课程", description = "对课程进行收藏或取消收藏操作")
    public Result<CourseLikeFavoriteDto.FavoriteResponse> toggleFavorite(
            @Parameter(description = "课程ID", example = "1") @PathVariable Long courseId) {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            CourseLikeFavoriteDto.FavoriteResponse response = courseLikeFavoriteService.toggleFavorite(userId, courseId);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("课程收藏操作失败 - courseId: {}", courseId, e);
            return Result.error(500, "操作失败");
        }
    }

    @GetMapping("/{courseId}/status")
    @Operation(summary = "获取课程点赞收藏状态", description = "获取课程的点赞和收藏状态信息")
    public Result<CourseLikeFavoriteDto.CourseStatusResponse> getCourseStatus(
            @Parameter(description = "课程ID", example = "1") @PathVariable Long courseId) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        
        try {
            CourseLikeFavoriteDto.CourseStatusResponse response = courseLikeFavoriteService.getCourseStatus(userId, courseId);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("获取课程状态失败 - userId: {}, courseId: {}", userId, courseId, e);
            return Result.error(500, "获取失败");
        }
    }

    @GetMapping("/liked")
    @Operation(summary = "获取用户点赞的课程列表", description = "获取当前用户点赞的课程记录列表")
    public Result<CourseLikeFavoriteDto.CourseListResponse> getUserLikedCourses(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            CourseLikeFavoriteDto.CourseListResponse response = courseLikeFavoriteService.getUserLikedCourses(userId, page, size);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("获取用户点赞课程列表失败", e);
            return Result.error(500, "获取失败");
        }
    }

    @GetMapping("/favorited")
    @Operation(summary = "获取用户收藏的课程列表", description = "获取当前用户收藏的课程记录列表")
    public Result<CourseLikeFavoriteDto.CourseListResponse> getUserFavoritedCourses(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            CourseLikeFavoriteDto.CourseListResponse response = courseLikeFavoriteService.getUserFavoritedCourses(userId, page, size);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("获取用户收藏课程列表失败", e);
            return Result.error(500, "获取失败");
        }
    }

    @GetMapping("/stats")
    @Operation(summary = "获取用户课程统计", description = "获取当前用户的课程点赞和收藏统计信息")
    public Result<CourseLikeFavoriteDto.CourseStatsResponse> getUserStats() {
        
        try {
            Long userId = SecurityUtil.getCurrentUserId();
            CourseLikeFavoriteDto.CourseStatsResponse response = courseLikeFavoriteService.getUserStats(userId);
            if (response.getSuccess()) {
                return Result.success(response);
            } else {
                return Result.error(400, response.getMessage());
            }
        } catch (Exception e) {
            log.error("获取用户课程统计失败", e);
            return Result.error(500, "获取失败");
        }
    }
}