package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreCuisineCategory;
import shop.alien.entity.store.dto.StoreCuisineCategoryDTO;
import shop.alien.entity.store.dto.StoreCuisineCategorySortDTO;
import shop.alien.store.service.StoreCuisineCategoryService;

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

/**
 * 菜品分类管理 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"商家端点餐管理-菜品分类管理"})
@ApiSort(14)
@CrossOrigin
@RestController
@RequestMapping("/storeCuisineCategory")
@RequiredArgsConstructor
public class StoreCuisineCategoryController {

    private final StoreCuisineCategoryService storeCuisineCategoryService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("查询菜品分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getCategoryList")
    public R<List<StoreCuisineCategory>> getCategoryList(@RequestParam Integer storeId) {
        log.info("StoreCuisineCategoryController.getCategoryList?storeId={}", storeId);
        return R.data(storeCuisineCategoryService.getCategoryList(storeId));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("批量创建菜品分类")
    @PostMapping("/batchCreateCategories")
    public R<Boolean> batchCreateCategories(@RequestBody StoreCuisineCategoryDTO dto) {
        log.info("StoreCuisineCategoryController.batchCreateCategories?dto={}", dto);
        
        if (dto.getStoreId() == null || !StringUtils.hasText(dto.getCategoryNames())) {
            return R.fail("门店ID和分类名称列表不能为空");
        }

        // 解析分类名称列表
        List<String> categoryNameList = Arrays.stream(dto.getCategoryNames().split(","))
                .map(String::trim)
                .filter(StringUtils::hasText)
                .distinct()
                .collect(Collectors.toList());

        if (categoryNameList.isEmpty()) {
            return R.fail("分类名称列表不能为空");
        }

        try {
            boolean result = storeCuisineCategoryService.batchCreateCategories(dto.getStoreId(), categoryNameList);
            if (result) {
                return R.success("批量创建菜品分类成功");
            } else {
                return R.fail("批量创建菜品分类失败");
            }
        } catch (Exception e) {
            log.error("批量创建菜品分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("编辑菜品分类")
    @PostMapping("/updateCategory")
    public R<Boolean> updateCategory(@RequestBody StoreCuisineCategoryDTO dto) {
        log.info("StoreCuisineCategoryController.updateCategory?dto={}", dto);
        
        if (dto.getId() == null || !StringUtils.hasText(dto.getCategoryName())) {
            return R.fail("分类ID和分类名称不能为空");
        }
        
        try {
            boolean result = storeCuisineCategoryService.updateCategory(dto.getId(), dto.getCategoryName());
            if (result) {
                return R.success("更新菜品分类成功");
            } else {
                return R.fail("更新菜品分类失败");
            }
        } catch (Exception e) {
            log.error("更新菜品分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("删除菜品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类ID", dataType = "Integer", paramType = "query", required = true)
    })
    @PostMapping("/deleteCategory")
    public R<Boolean> deleteCategory(@RequestParam Integer id) {
        log.info("StoreCuisineCategoryController.deleteCategory?id={}", id);
        
        try {
            boolean result = storeCuisineCategoryService.deleteCategory(id);
            if (result) {
                return R.success("删除菜品分类成功");
            } else {
                return R.fail("删除菜品分类失败");
            }
        } catch (Exception e) {
            log.error("删除菜品分类失败", e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("更新菜品分类排序")
    @PostMapping("/updateCategorySort")
    public R<Boolean> updateCategorySort(@RequestBody StoreCuisineCategorySortDTO dto) {
        log.info("StoreCuisineCategoryController.updateCategorySort?dto={}", dto);
        
        if (dto.getStoreId() == null || dto.getCategoryIds() == null || dto.getCategoryIds().isEmpty()) {
            return R.fail("门店ID和分类ID列表不能为空");
        }
        
        try {
            boolean result = storeCuisineCategoryService.updateCategorySort(dto.getStoreId(), dto.getCategoryIds());
            if (result) {
                return R.success("更新菜品分类排序成功");
            } else {
                return R.fail("更新菜品分类排序失败");
            }
        } catch (Exception e) {
            log.error("更新菜品分类排序失败", e);
            return R.fail(e.getMessage());
        }
    }
}
