package com.snack.server.module.category.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.snack.server.common.Result;
import com.snack.server.module.category.dto.req.CategorySaveReq;
import com.snack.server.module.category.entity.Category;
import com.snack.server.module.category.service.CategoryService;
import com.snack.server.module.category.vo.CategoryOptionVO;
import com.snack.server.module.category.vo.CategoryTreeVO;
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.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类管理（管理端）
 */
@Tag(name = "02. 商品分类")
@RestController
@RequestMapping("/api/admin/category")
@RequiredArgsConstructor
public class CategoryAdminController {

    private final CategoryService categoryService;

    @Operation(summary = "获取分类树")
    @SaCheckLogin
    @GetMapping("/tree")
    public Result<List<CategoryTreeVO>> tree(
            @Parameter(description = "状态过滤：null-全部 0-禁用 1-启用")
            @RequestParam(required = false) Integer status) {
        return Result.ok(categoryService.listTree(status));
    }

    @Operation(summary = "获取某父分类下的直接子分类")
    @SaCheckLogin
    @GetMapping("/children")
    public Result<List<Category>> listChildren(
            @Parameter(description = "父分类 ID，0=顶级")
            @RequestParam(defaultValue = "0") Long parentId) {
        return Result.ok(categoryService.listByParentId(parentId));
    }

    @Operation(summary = "获取启用状态的下拉选项")
    @SaCheckLogin
    @GetMapping("/options")
    public Result<List<CategoryOptionVO>> options() {
        return Result.ok(categoryService.listOptions());
    }

    @Operation(summary = "获取分类详情")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<Category> detail(@Parameter(description = "分类 ID") @PathVariable Long id) {
        return Result.ok(categoryService.getById(id));
    }

    @Operation(summary = "创建分类")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody CategorySaveReq req) {
        return Result.ok(categoryService.create(req));
    }

    @Operation(summary = "更新分类")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody CategorySaveReq req) {
        categoryService.update(req);
        return Result.ok();
    }

    @Operation(summary = "删除分类")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "分类 ID") @PathVariable Long id) {
        categoryService.delete(id);
        return Result.ok();
    }

    @Operation(summary = "启用 / 禁用分类")
    @SaCheckLogin
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateStatus(
            @Parameter(description = "分类 ID") @PathVariable Long id,
            @Parameter(description = "状态：0-禁用 1-启用") @PathVariable Integer status) {
        categoryService.updateStatus(id, status);
        return Result.ok();
    }

    @Operation(summary = "调整排序")
    @SaCheckLogin
    @PutMapping("/{id}/sort/{sort}")
    public Result<Void> updateSort(
            @Parameter(description = "分类 ID") @PathVariable Long id,
            @Parameter(description = "排序号") @PathVariable Integer sort) {
        categoryService.updateSort(id, sort);
        return Result.ok();
    }

    @Operation(summary = "上传分类图标")
    @SaCheckLogin
    @PostMapping("/{id}/icon")
    public Result<com.snack.server.module.file.vo.FileUploadVO> uploadIcon(
            @Parameter(description = "分类 ID") @PathVariable Long id,
            @org.springframework.web.bind.annotation.RequestParam("file") org.springframework.web.multipart.MultipartFile file) {
        com.snack.server.module.file.enums.FileBizTypeEnum biz = com.snack.server.module.file.enums.FileBizTypeEnum.CATEGORY;
        com.snack.server.module.file.vo.FileUploadVO vo =
                com.snack.server.common.utils.SpringContextHolder.getBean(com.snack.server.module.file.service.FileService.class)
                        .uploadWithUploader(file, biz.getCode(), null);
        // 同步更新分类的 icon 字段
        categoryService.updateIcon(id, vo.getUrl());
        return Result.ok(vo);
    }
}
