package com.dora.controller;

import com.dora.dto.WorkflowLikeDto;
import com.dora.service.WorkflowLikeService;
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/v1/workflow-likes")
@RequiredArgsConstructor
@Tag(name = "工作流点赞管理", description = "工作流点赞相关接口")
public class WorkflowLikeController {

    private final WorkflowLikeService workflowLikeService;

    @PostMapping("/toggle")
    @Operation(summary = "点赞/取消点赞工作流", description = "对工作流进行点赞或取消点赞操作")
    public WorkflowLikeDto.LikeResponse toggleLike(
            @RequestBody WorkflowLikeDto.LikeRequest request) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        return workflowLikeService.toggleLike(userId, request.getWorkflowId());
    }

    @GetMapping("/check")
    @Operation(summary = "检查工作流点赞状态", description = "检查用户是否已点赞指定工作流")
    public WorkflowLikeDto.LikeResponse checkLikeStatus(
            @Parameter(description = "工作流ID", example = "1") @RequestParam Long workflowId) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        boolean isLiked = workflowLikeService.isLiked(userId, workflowId);
        Integer likeCount = workflowLikeService.getLikeCount(workflowId);
        
        WorkflowLikeDto.LikeResponse response = new WorkflowLikeDto.LikeResponse();
        response.setSuccess(true);
        response.setMessage("查询成功");
        response.setIsLiked(isLiked);
        response.setLikeCount(likeCount);
        
        return response;
    }

    @GetMapping("/count")
    @Operation(summary = "获取工作流点赞数量", description = "获取指定工作流的点赞数量")
    public WorkflowLikeDto.LikeResponse getLikeCount(
            @Parameter(description = "工作流ID", example = "1") @RequestParam Long workflowId) {
        
        Integer likeCount = workflowLikeService.getLikeCount(workflowId);
        
        WorkflowLikeDto.LikeResponse response = new WorkflowLikeDto.LikeResponse();
        response.setSuccess(true);
        response.setMessage("查询成功");
        response.setIsLiked(false); // 这里不关心用户是否点赞
        response.setLikeCount(likeCount);
        
        return response;
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户工作流点赞列表", description = "获取当前用户点赞的工作流记录列表")
    public WorkflowLikeDto.LikeListResponse getUserLikes(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        return workflowLikeService.getUserLikes(userId, page, size);
    }

    @GetMapping("/user/stats")
    @Operation(summary = "获取用户工作流点赞统计", description = "获取当前用户的工作流点赞统计信息")
    public WorkflowLikeDto.LikeStatsResponse getUserLikeStats() {
        
        Long userId = SecurityUtil.getCurrentUserId();
        return workflowLikeService.getUserLikeStats(userId);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取工作流点赞统计", description = "获取工作流点赞统计信息")
    public WorkflowLikeDto.LikeStatsResponse getWorkflowLikeStats() {
        
        Long userId = SecurityUtil.getCurrentUserId();
        return workflowLikeService.getUserLikeStats(userId);
    }
} 