package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StorePlatformMenu;
import shop.alien.storeplatform.service.StorePlatformMenuService;

import java.util.List;

/**
 * 商家PC菜单权限表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"平台-菜单管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/platform/menu")
@RequiredArgsConstructor
public class StorePlatformMenuController {

    private final StorePlatformMenuService storePlatformMenuService;

    @ApiOperation("分页查询菜单列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "menuName", value = "菜单名称（支持模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "menuType", value = "菜单类型（M目录 C菜单 F按钮）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "菜单状态（0正常 1停用）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getMenuPage")
    public R<IPage<StorePlatformMenu>> getMenuPage(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(value = "menuName", required = false) String menuName,
            @RequestParam(value = "menuType", required = false) String menuType,
            @RequestParam(value = "status", required = false) String status) {
        log.info("StorePlatformMenuController.getMenuPage?page={}, size={}, menuName={}, menuType={}, status={}", 
                page, size, menuName, menuType, status);
        IPage<StorePlatformMenu> menuPage = storePlatformMenuService.getMenuPage(page, size, menuName, menuType, status);
        return R.data(menuPage);
    }

    @ApiOperation("根据ID查询菜单详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getMenuById")
    public R<StorePlatformMenu> getMenuById(@RequestParam("menuId") Long menuId) {
        log.info("StorePlatformMenuController.getMenuById?menuId={}", menuId);
        StorePlatformMenu menu = storePlatformMenuService.getMenuById(menuId);
        if (menu == null) {
            return R.fail("未找到该菜单信息");
        }
        return R.data(menu);
    }

    @ApiOperation("新增菜单")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveMenu")
    public R<String> saveMenu(@RequestBody StorePlatformMenu menu) {
        log.info("StorePlatformMenuController.saveMenu?menu={}", menu);
        boolean result = storePlatformMenuService.saveMenu(menu);
        if (result) {
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("修改菜单")
    @ApiOperationSupport(order = 4)
    @PostMapping("/updateMenu")
    public R<String> updateMenu(@RequestBody StorePlatformMenu menu) {
        log.info("StorePlatformMenuController.updateMenu?menu={}", menu);
        boolean result = storePlatformMenuService.updateMenu(menu);
        if (result) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("删除菜单")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true)
    })
    @DeleteMapping("/deleteMenu")
    public R<String> deleteMenu(@RequestParam("menuId") Long menuId) {
        log.info("StorePlatformMenuController.deleteMenu?menuId={}", menuId);
        boolean result = storePlatformMenuService.deleteMenu(menuId);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("修改菜单状态")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "菜单状态（0正常 1停用）", dataType = "String", paramType = "query", required = true)
    })
    @PutMapping("/updateStatus")
    public R<String> updateStatus(
            @RequestParam("menuId") Long menuId,
            @RequestParam("status") String status) {
        log.info("StorePlatformMenuController.updateStatus?menuId={}, status={}", menuId, status);
        boolean result = storePlatformMenuService.updateStatus(menuId, status);
        if (result) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("查询所有正常状态的菜单列表（树形结构）")
    @ApiOperationSupport(order = 7)
    @GetMapping("/getMenuTree")
    public R<List<StorePlatformMenu>> getMenuTree() {
        log.info("StorePlatformMenuController.getMenuTree");
        List<StorePlatformMenu> menus = storePlatformMenuService.getMenuTree();
        return R.data(menus);
    }

    @ApiOperation("根据父菜单ID查询子菜单列表")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父菜单ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getMenusByParentId")
    public R<List<StorePlatformMenu>> getMenusByParentId(@RequestParam("parentId") Long parentId) {
        log.info("StorePlatformMenuController.getMenusByParentId?parentId={}", parentId);
        List<StorePlatformMenu> menus = storePlatformMenuService.getMenusByParentId(parentId);
        return R.data(menus);
    }

    @ApiOperation("根据层级查询菜单列表")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "菜单层级（1一级菜单 2二级菜单 3三级菜单）", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/getMenusByLevel")
    public R<List<StorePlatformMenu>> getMenusByLevel(@RequestParam("level") Integer level) {
        log.info("StorePlatformMenuController.getMenusByLevel?level={}", level);
        List<StorePlatformMenu> menus = storePlatformMenuService.getMenusByLevel(level);
        return R.data(menus);
    }
}
