package com.lesingle.edu.controller.admin;

import com.lesingle.edu.common.annotation.Log;
import com.lesingle.edu.common.enums.LogModule;
import com.lesingle.edu.common.enums.LogOperationType;
import com.lesingle.edu.common.annotation.RequireRole;
import com.lesingle.edu.common.enums.UserRole;
import com.lesingle.edu.common.response.Result;
import com.lesingle.edu.dto.request.ThemeCreateRequest;
import com.lesingle.edu.dto.response.ThemeResponse;
import com.lesingle.edu.entity.Theme;
import com.lesingle.edu.service.ThemeService;
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.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 主题字典控制器（超管端）
 */
@RestController
@RequestMapping("/api/v1/admin/themes")
@RequiredArgsConstructor
@Tag(name = "超管端 - 主题字典")
public class AdminThemeController {

    private final ThemeService themeService;

    @GetMapping
    @Operation(summary = "查询所有主题")
    public Result<List<ThemeResponse>> findAll() {
        List<Theme> themes = themeService.findAll();
        List<ThemeResponse> responses = themes.stream()
                .map(this::toResponse)
                .collect(Collectors.toList());
        return Result.success(responses);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询主题详情")
    public Result<ThemeResponse> findOne(@PathVariable Long id) {
        Theme theme = themeService.findById(id);
        return Result.success(toResponse(theme));
    }

    @PostMapping
    @Operation(summary = "创建主题")
    @Log(module = LogModule.THEME, type = LogOperationType.CREATE, description = "创建主题")
    @RequireRole(UserRole.ADMIN)
    public Result<ThemeResponse> create(@Valid @RequestBody ThemeCreateRequest request) {
        Theme theme = themeService.create(
                request.getName(),
                request.getDescription(),
                request.getColor(),
                request.getSortOrder()
        );
        return Result.success(toResponse(theme));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新主题")
    @Log(module = LogModule.THEME, type = LogOperationType.UPDATE, description = "更新主题")
    @RequireRole(UserRole.ADMIN)
    public Result<ThemeResponse> update(
            @PathVariable Long id,
            @RequestBody ThemeCreateRequest request) {
        Theme theme = themeService.update(
                id,
                request.getName(),
                request.getDescription(),
                request.getColor(),
                request.getSortOrder(),
                null
        );
        return Result.success(toResponse(theme));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除主题")
    @Log(module = LogModule.THEME, type = LogOperationType.DELETE, description = "删除主题")
    @RequireRole(UserRole.ADMIN)
    public Result<Void> delete(@PathVariable Long id) {
        themeService.delete(id);
        return Result.success();
    }

    @PutMapping("/reorder")
    @Operation(summary = "重新排序主题")
    @Log(module = LogModule.THEME, type = LogOperationType.UPDATE, description = "重新排序主题")
    @RequireRole(UserRole.ADMIN)
    public Result<Void> reorder(@RequestBody List<Long> ids) {
        themeService.reorder(ids);
        return Result.success();
    }

    /**
     * 将 Theme 实体转换为 ThemeResponse
     */
    private ThemeResponse toResponse(Theme theme) {
        return ThemeResponse.builder()
                .id(theme.getId())
                .name(theme.getName())
                .description(theme.getDescription())
                .color(theme.getColor())
                .sortOrder(theme.getSortOrder())
                .status(theme.getStatus())
                .createdAt(theme.getCreatedAt())
                .updatedAt(theme.getUpdatedAt())
                .build();
    }
}