package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.PageResultDto;
import com.dora.dto.WorkflowListItemDto;
import com.dora.dto.CourseListItemDto;
import com.dora.service.WorkflowService;
import com.dora.service.CourseService;
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;

/**
 * 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Tag(name = "测试接口", description = "用于测试各种功能的接口")
public class TestController {

    private final WorkflowService workflowService;
    private final CourseService courseService;

    @GetMapping("/workflow/categories")
    @Operation(summary = "测试获取工作流分类列表")
    public Result<List<com.dora.dto.WorkflowCategoryDto>> testWorkflowCategories() {
        try {
            log.info("测试获取工作流分类列表");
            List<com.dora.dto.WorkflowCategoryDto> categories = workflowService.getCategories();
            log.info("工作流分类列表: {}", categories);
            return Result.success(categories);
        } catch (Exception e) {
            log.error("测试获取工作流分类列表失败", e);
            return Result.error(500, "测试获取工作流分类列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/workflow/hot")
    @Operation(summary = "测试获取热门工作流列表")
    public Result<PageResultDto<WorkflowListItemDto>> testHotWorkflows(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size) {
        try {
            log.info("测试获取热门工作流列表 - page: {}, size: {}", page, size);
            PageResultDto<WorkflowListItemDto> result = workflowService.getHotWorkflows(page, size);
            log.info("热门工作流列表: {}", result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("测试获取热门工作流列表失败", e);
            return Result.error(500, "测试获取热门工作流列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/course/categories")
    @Operation(summary = "测试获取课程分类列表")
    public Result<List<com.dora.dto.CourseCategoryDto>> testCourseCategories() {
        try {
            log.info("测试获取课程分类列表");
            List<com.dora.dto.CourseCategoryDto> categories = courseService.getCategories();
            log.info("课程分类列表: {}", categories);
            return Result.success(categories);
        } catch (Exception e) {
            log.error("测试获取课程分类列表失败", e);
            return Result.error(500, "测试获取课程分类列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/course/hot")
    @Operation(summary = "测试获取热门课程列表")
    public Result<PageResultDto<CourseListItemDto>> testHotCourses(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size) {
        try {
            log.info("测试获取热门课程列表 - page: {}, size: {}", page, size);
            PageResultDto<CourseListItemDto> result = courseService.getHotCourses(page, size);
            log.info("热门课程列表: {}", result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("测试获取热门课程列表失败", e);
            return Result.error(500, "测试获取热门课程列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/workflow/category/{category}")
    @Operation(summary = "测试根据分类获取工作流列表")
    public Result<PageResultDto<WorkflowListItemDto>> testWorkflowsByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size) {
        try {
            log.info("测试根据分类获取工作流列表 - category: {}, page: {}, size: {}", category, page, size);
            PageResultDto<WorkflowListItemDto> result = workflowService.getWorkflowsByCategory(category, page, size);
            log.info("分类工作流列表: {}", result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("测试根据分类获取工作流列表失败", e);
            return Result.error(500, "测试根据分类获取工作流列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/course/category/{category}")
    @Operation(summary = "测试根据分类获取课程列表")
    public Result<PageResultDto<CourseListItemDto>> testCoursesByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size) {
        try {
            log.info("测试根据分类获取课程列表 - category: {}, page: {}, size: {}", category, page, size);
            PageResultDto<CourseListItemDto> result = courseService.getCoursesByCategory(category, page, size);
            log.info("分类课程列表: {}", result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("测试根据分类获取课程列表失败", e);
            return Result.error(500, "测试根据分类获取课程列表失败: " + e.getMessage());
        }
    }
} 