package com.zioinfo.signage.uiws.system.controller;

import com.zioinfo.signage.common.ApiResponse;
import com.zioinfo.signage.uiws.system.dto.IdsRequest;
import com.zioinfo.signage.uiws.system.dto.MenuDto;
import com.zioinfo.signage.uiws.system.dto.MenuSaveDto;
import com.zioinfo.signage.uiws.system.dto.PageResponse;
import com.zioinfo.signage.uiws.system.service.MenuService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 2.9 메뉴(menus) — 트리목록/검색/CRUD/다중삭제. base: /api/system/menus */
@RestController
@RequestMapping("/api/system/menus")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService menuService;

    @GetMapping
    public ApiResponse<PageResponse<MenuDto>> list(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "100") int size) {
        return ApiResponse.ok(menuService.list(page, size));
    }

    @GetMapping("/search")
    public ApiResponse<List<MenuDto>> search(@RequestParam(required = false) String keyword) {
        return ApiResponse.ok(menuService.searchPopup(keyword));
    }

    @PostMapping
    public ApiResponse<MenuDto> create(@Valid @RequestBody MenuSaveDto dto) {
        return ApiResponse.ok(menuService.create(dto));
    }

    @GetMapping("/{id}")
    public ApiResponse<MenuDto> get(@PathVariable("id") String id) {
        return ApiResponse.ok(menuService.get(id));
    }

    @PutMapping("/{id}")
    public ApiResponse<MenuDto> update(@PathVariable("id") String id, @Valid @RequestBody MenuSaveDto dto) {
        return ApiResponse.ok(menuService.update(id, dto));
    }

    @DeleteMapping
    public ApiResponse<Void> delete(@Valid @RequestBody IdsRequest req) {
        menuService.delete(req.ids());
        return ApiResponse.ok(null);
    }
}
