package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.common.Result;
import com.dora.dto.CategoryCreateDto;
import com.dora.dto.CategoryDto;
import com.dora.dto.CategoryListDto;
import com.dora.dto.CategoryUpdateDto;
import com.dora.dto.PageResultDto;
import com.dora.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 管理员类目管理控制器
 */
@RestController
@RequestMapping("/admin/categories")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员类目管理", description = "管理员类目管理相关接口")
@RequireAdminOrStaff
public class AdminCategoryController {

    private final CategoryService categoryService;

    @GetMapping("/list")
    @Operation(summary = "分页获取类目列表", description = "管理员分页查询类目列表，支持按类型筛选")
    public Result<PageResultDto<CategoryListDto>> getCategoriesByPage(
            @Parameter(description = "类目类型(1课程分类/2工作流分类)，不传则查询所有") @RequestParam(required = false) Integer type,
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") int size) {
        
        log.info("管理员查询类目列表: type={}, page={}, size={}", type, page, size);
        PageResultDto<CategoryListDto> result = categoryService.getCategoriesByPage(type, page, size);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取类目详情", description = "根据ID获取类目详细信息")
    public Result<CategoryDto> getCategoryById(
            @Parameter(description = "类目ID") @PathVariable Long id) {
        
        log.info("管理员查询类目详情: id={}", id);
        CategoryDto result = categoryService.getCategoryById(id);
        return Result.success(result);
    }

    @PostMapping("/create")
    @Operation(summary = "创建类目", description = "管理员创建新类目")
    public Result<CategoryDto> createCategory(@Valid @RequestBody CategoryCreateDto createDto) {
        
        log.info("管理员创建类目: {}", createDto);
        CategoryDto result = categoryService.createCategory(createDto);
        return Result.success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新类目", description = "管理员更新类目信息")
    public Result<CategoryDto> updateCategory(@Valid @RequestBody CategoryUpdateDto updateDto) {
        
        log.info("管理员更新类目: {}", updateDto);
        CategoryDto result = categoryService.updateCategory(updateDto);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除类目", description = "管理员删除类目")
    public Result<Void> deleteCategory(
            @Parameter(description = "类目ID") @PathVariable Long id) {
        
        log.info("管理员删除类目: id={}", id);
        categoryService.deleteCategory(id);
        return Result.success(null);
    }

    @PutMapping("/batch-sort")
    @Operation(summary = "批量更新排序", description = "批量更新类目排序")
    public Result<Void> batchUpdateSortOrder(@Valid @RequestBody List<CategoryUpdateDto> updateDtos) {
        
        log.info("管理员批量更新类目排序: size={}", updateDtos.size());
        categoryService.batchUpdateSortOrder(updateDtos);
        return Result.success(null);
    }

    @GetMapping("/check-name")
    @Operation(summary = "检查类目名称", description = "检查指定类型下类目名称是否已存在")
    public Result<Boolean> checkCategoryName(
            @Parameter(description = "类目名称") @RequestParam String name,
            @Parameter(description = "类目类型(1课程分类/2工作流分类)") @RequestParam Integer type,
            @Parameter(description = "排除的类目ID（用于更新时检查）") @RequestParam(required = false) Long excludeId) {
        
        log.info("管理员检查类目名称: name={}, type={}, excludeId={}", name, type, excludeId);
        boolean exists = categoryService.isNameExists(name, type, excludeId);
        return Result.success(exists);
    }

    @PostMapping("/migrate-course-data")
    @Operation(summary = "迁移课程分类数据", description = "将老的category字段数据同步到新的category_id字段")
    public Result<String> migrateCourseData() {
        
        log.info("收到课程分类数据迁移请求");
        String result = categoryService.migrateCourseData();
        return Result.success(result);
    }
}
