package com.ga.common.menu;

import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "메뉴")
@RestController
@RequestMapping("/api/menus")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService service;

    /** 로그인 사용자의 메뉴 (좌측 사이드바용) */
    @GetMapping("/my")
    public ApiResponse<List<MenuResp>> myMenus() {
        return ApiResponse.ok(service.getMyMenus());
    }

    @GetMapping("/tree")
    @RequirePermission(menu = "SYSTEM_MENU", perm = "READ")
    public ApiResponse<List<MenuResp>> tree() {
        return ApiResponse.ok(service.getMenuTree());
    }

    @GetMapping("/{menuId}")
    @RequirePermission(menu = "SYSTEM_MENU", perm = "READ")
    public ApiResponse<MenuVO> detail(@PathVariable Long menuId) {
        return ApiResponse.ok(service.getById(menuId));
    }

    @PostMapping
    @RequirePermission(menu = "SYSTEM_MENU", perm = "CREATE")
    public ApiResponse<Long> create(@RequestBody MenuVO vo) {
        return ApiResponse.ok(service.create(vo));
    }

    @PutMapping("/{menuId}")
    @RequirePermission(menu = "SYSTEM_MENU", perm = "UPDATE")
    public ApiResponse<Void> update(@PathVariable Long menuId, @RequestBody MenuVO vo) {
        service.update(menuId, vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/{menuId}")
    @RequirePermission(menu = "SYSTEM_MENU", perm = "DELETE")
    public ApiResponse<Void> delete(@PathVariable Long menuId) {
        service.delete(menuId);
        return ApiResponse.ok();
    }
}
