package cn.meowrain.aioj.backend.question.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.question.dao.entity.Question;
import cn.meowrain.aioj.backend.question.dto.req.QuestionCreateRequestDTO;
import cn.meowrain.aioj.backend.question.dto.req.QuestionEditRequestDTO;
import cn.meowrain.aioj.backend.question.dto.req.QuestionQueryRequestDTO;
import cn.meowrain.aioj.backend.question.dto.resp.QuestionResponseDTO;
import cn.meowrain.aioj.backend.question.service.QuestionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 题目管理控制器 - RESTful API
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/questions")
@Tag(name = "题目管理", description = "题目增删改查等接口")
public class QuestionController {

	private final QuestionService questionService;

	/**
	 * 创建题目
	 * POST /v1/questions
	 */
	@PostMapping
	@Operation(summary = "创建题目", description = "创建新的编程题目")
	public Result<Long> createQuestion(
			@Parameter(description = "题目信息", required = true)
			@RequestBody @Valid QuestionCreateRequestDTO request) {
		Long questionId = questionService.createQuestionWithChain(request);
		return Results.success(questionId);
	}

	/**
	 * 更新题目
	 * PUT /v1/questions/{id}
	 */
	@PutMapping("/{id}")
	@Operation(summary = "更新题目", description = "根据ID更新题目信息")
	public Result<Void> updateQuestion(
			@Parameter(description = "题目ID", required = true)
			@PathVariable("id") Long id,
			@Parameter(description = "题目信息", required = true)
			@RequestBody @Valid QuestionEditRequestDTO request) {
		questionService.updateQuestionWithChain(id, request);
		return Results.success();
	}

	/**
	 * 删除题目
	 * DELETE /v1/questions/{id}
	 */
	@DeleteMapping("/{id}")
	@Operation(summary = "删除题目", description = "根据ID删除题目")
	public Result<Void> deleteQuestion(
			@Parameter(description = "题目ID", required = true)
			@PathVariable("id") Long id) {
		questionService.deleteQuestion(id);
		return Results.success();
	}

	/**
	 * 获取题目详情
	 * GET /v1/questions/{id}
	 */
	@GetMapping("/{id}")
	@Operation(summary = "获取题目详情", description = "根据ID获取题目详情")
	public Result<QuestionResponseDTO> getQuestion(
			@Parameter(description = "题目ID", required = true)
			@PathVariable("id") Long id) {
		QuestionResponseDTO question = questionService.getQuestionById(id);
		return Results.success(question);
	}

	/**
	 * 分页查询题目列表
	 * GET /v1/questions
	 */
	@GetMapping
	@Operation(summary = "分页查询题目列表", description = "支持按标题、难度、标签等条件查询")
	public Result<Page<QuestionResponseDTO>> listQuestions(
			@Parameter(description = "查询条件") QuestionQueryRequestDTO request) {
		Page<QuestionResponseDTO> page = questionService.listQuestions(request);
		return Results.success(page);
	}

}
