package com.lesingle.edu.controller.school;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.edu.common.response.PageResult;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.common.security.SecurityUtils;
import com.lesingle.edu.dto.response.LessonFeedbackResponse;
import com.lesingle.edu.entity.LessonFeedback;
import com.lesingle.edu.service.LessonFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 学校端 - 反馈管理
 */
@Slf4j
@Tag(name = "学校端 - 反馈管理", description = "School Feedback APIs")
@RestController
@RequestMapping("/api/v1/school/feedbacks")
@RequiredArgsConstructor
public class SchoolFeedbackController {

    private final LessonFeedbackService lessonFeedbackService;

    @GetMapping
    @Operation(summary = "获取反馈列表")
    public Result<PageResult<LessonFeedbackResponse>> getFeedbacks(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long teacherId,
            @RequestParam(required = false) Long courseId,
            @RequestParam(required = false) String keyword) {

        // 获取当前租户 ID
        Long tenantId = SecurityUtils.getCurrentTenantId();

        // 调用 Service 层查询
        Page<LessonFeedback> page = lessonFeedbackService.getFeedbackPage(
                tenantId, pageNum, pageSize, teacherId, courseId, keyword
        );

        // Entity 转为 Response
        List<LessonFeedbackResponse> voList = page.getRecords().stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());

        PageResult<LessonFeedbackResponse> result = new PageResult<>();
        result.setList(voList);
        result.setTotal(page.getTotal());
        result.setPageNum(page.getCurrent());
        result.setPageSize(page.getSize());
        result.setPages((page.getTotal() + pageSize - 1) / pageSize);

        return Result.success(result);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取反馈统计")
    public Result<Map<String, Object>> getFeedbackStats() {
        // 获取当前租户 ID
        Long tenantId = SecurityUtils.getCurrentTenantId();

        // 调用 Service 层查询
        Map<String, Object> stats = lessonFeedbackService.getFeedbackStats(tenantId);
        return Result.success(stats);
    }

    /**
     * Entity 转 Response
     */
    private LessonFeedbackResponse convertToResponse(LessonFeedback feedback) {
        return LessonFeedbackResponse.builder()
                .id(feedback.getId())
                .lessonId(feedback.getLessonId())
                .teacherId(feedback.getTeacherId())
                .content(feedback.getContent())
                .rating(feedback.getRating())
                .designQuality(feedback.getDesignQuality())
                .participation(feedback.getParticipation())
                .goalAchievement(feedback.getGoalAchievement())
                .stepFeedbacks(feedback.getStepFeedbacks())
                .pros(feedback.getPros())
                .suggestions(feedback.getSuggestions())
                .activitiesDone(feedback.getActivitiesDone())
                .createdAt(feedback.getCreatedAt())
                .build();
    }
}
