package com.reading.platform.controller.teacher;

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.common.security.SecurityUtils;
import com.reading.platform.dto.request.TaskCreateRequest;
import com.reading.platform.dto.request.TaskUpdateRequest;
import com.reading.platform.entity.Task;
import com.reading.platform.service.TaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Teacher - Task", description = "Task APIs for Teacher")
@RestController
@RequestMapping("/api/teacher/tasks")
@RequiredArgsConstructor
public class TeacherTaskController {

    private final TaskService taskService;

    @Operation(summary = "Create task")
    @PostMapping
    public Result<Task> createTask(@Valid @RequestBody TaskCreateRequest request) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Long userId = SecurityUtils.getCurrentUserId();
        return Result.success(taskService.createTask(tenantId, userId, "teacher", request));
    }

    @Operation(summary = "Update task")
    @PutMapping("/{id}")
    public Result<Task> updateTask(@PathVariable Long id, @RequestBody TaskUpdateRequest request) {
        return Result.success(taskService.updateTask(id, request));
    }

    @Operation(summary = "Get task by ID")
    @GetMapping("/{id}")
    public Result<Task> getTask(@PathVariable Long id) {
        return Result.success(taskService.getTaskById(id));
    }

    @Operation(summary = "Get task page")
    @GetMapping
    public Result<PageResult<Task>> getTaskPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status) {
        Long tenantId = SecurityUtils.getCurrentTenantId();
        Page<Task> page = taskService.getTaskPage(tenantId, pageNum, pageSize, keyword, type, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete task")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTask(@PathVariable Long id) {
        taskService.deleteTask(id);
        return Result.success();
    }

}
