package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.system.dto.CreateMenuRequest;
import com.writeoff.module.system.dto.ReorderMenusRequest;
import com.writeoff.module.system.dto.UpdateMenuRequest;
import com.writeoff.module.system.model.MenuInfo;
import com.writeoff.module.system.service.MenuService;
import com.writeoff.security.AuthContext;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/api/menus")
public class MenuController {
    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping
    @RequirePermission(value = "role.read", dataScope = DataScopeType.TENANT, auditAction = "MENU_LIST")
    public ApiResponse<PageResult<MenuInfo>> list() {
        return ApiResponse.success(menuService.list());
    }

    @GetMapping("/current")
    public ApiResponse<List<MenuInfo>> current() {
        Long userId = AuthContext.userId();
        if (userId == null) {
            return ApiResponse.success(java.util.Collections.emptyList());
        }
        return ApiResponse.success(menuService.currentUserMenus(userId));
    }

    @PostMapping
    @RequirePermission(value = "role.permission.bind", dataScope = DataScopeType.TENANT, auditAction = "MENU_CREATE")
    public ApiResponse<MenuInfo> create(@RequestBody @Valid CreateMenuRequest request) {
        return ApiResponse.success(menuService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "role.permission.bind", dataScope = DataScopeType.TENANT, auditAction = "MENU_UPDATE")
    public ApiResponse<MenuInfo> update(@PathVariable("id") Long id, @RequestBody @Valid UpdateMenuRequest request) {
        return ApiResponse.success(menuService.update(id, request));
    }

    @PostMapping("/reorder")
    @RequirePermission(value = "role.permission.bind", dataScope = DataScopeType.TENANT, auditAction = "MENU_REORDER")
    public ApiResponse<String> reorder(@RequestBody @Valid ReorderMenusRequest request) {
        menuService.reorderMenus(request);
        return ApiResponse.success("ok");
    }

    @GetMapping("/{id}/roles")
    @RequirePermission(value = "role.read", dataScope = DataScopeType.TENANT, auditAction = "MENU_ROLES")
    public ApiResponse<List<String>> menuRoles(@PathVariable("id") Long id) {
        return ApiResponse.success(menuService.getMenuRoleNames(id));
    }
}
