package com.reading.platform.controller.parent;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.dto.request.TaskFeedbackUpdateRequest;
import com.reading.platform.dto.response.TaskFeedbackResponse;
import com.reading.platform.entity.Task;
import com.reading.platform.entity.TaskCompletion;
import com.reading.platform.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "家长 - 任务", description = "任务接口（家长专用）")
@RestController
@RequestMapping("/api/v1/parent/tasks")
@RequiredArgsConstructor
public class ParentTaskController {

    private final TaskService taskService;

    @Operation(summary = "根据 ID 获取任务")
    @GetMapping("/{id}")
    public Result<Task> getTask(@PathVariable String id) {
        return Result.success(taskService.getTaskById(id));
    }

    @Operation(summary = "根据学生 ID 获取任务")
    @GetMapping("/student/{studentId}")
    public Result<PageResult<Task>> getTasksByStudent(
            @PathVariable String studentId,
            @RequestParam(value = "page", required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String status) {
        Page<Task> page = taskService.getTasksByStudentId(studentId, pageNum, pageSize, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "完成任务")
    @PostMapping("/{taskId}/complete")
    public Result<Void> completeTask(
            @PathVariable String taskId,
            @RequestParam String studentId,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) String attachments) {
        taskService.completeTask(taskId, studentId, content, attachments);
        return Result.success();
    }

    @Operation(summary = "提交任务家长反馈")
    @PutMapping("/{childId}/tasks/{taskId}/feedback")
    public Result<TaskFeedbackResponse> submitParentFeedback(
            @PathVariable String childId,
            @PathVariable String taskId,
            @RequestBody TaskFeedbackUpdateRequest request) {
        String feedback = request.getFeedback();

        TaskCompletion completion = taskService.getTaskCompletion(taskId, childId);

        if (completion != null) {
            TaskFeedbackResponse response = new TaskFeedbackResponse();
            response.setId(completion.getId());
            response.setTaskId(completion.getTaskId());
            response.setStudentId(completion.getStudentId());
            response.setStatus(completion.getStatus());
            response.setParentFeedback(feedback);
            return Result.success(response);
        } else {
            TaskFeedbackResponse response = new TaskFeedbackResponse();
            response.setMessage("未找到任务完成记录");
            return Result.success(response);
        }
    }

}
