package com.zioinfo.cms.menu;

import com.zioinfo.cms.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cms/menu")
@RequiredArgsConstructor
public class MenuController {

    private final MenuService service;

    @GetMapping
    public ApiResponse<List<CmsMenu>> list(@RequestParam(required = false) String menuType) {
        return ApiResponse.ok(service.listFlat(menuType));
    }

    @GetMapping("/tree")
    public ApiResponse<List<CmsMenu>> tree(@RequestParam(required = false) String menuType) {
        return ApiResponse.ok(service.tree(menuType));
    }

    @GetMapping("/{id}")
    public ApiResponse<CmsMenu> get(@PathVariable Long id) {
        return ApiResponse.ok(service.get(id));
    }

    @PostMapping
    public ApiResponse<CmsMenu> create(@RequestBody Map<String, Object> req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/{id}")
    public ApiResponse<CmsMenu> update(@PathVariable Long id, @RequestBody Map<String, Object> req) {
        return ApiResponse.ok(service.update(id, req));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ApiResponse.ok(null);
    }

    /** 사이트맵 XML 생성. */
    @GetMapping("/sitemap")
    public ApiResponse<Map<String, String>> sitemap(@RequestParam(defaultValue = "https://cms.zioinfo.co.kr") String baseUrl) {
        return ApiResponse.ok(Map.of("xml", service.sitemapXml(baseUrl)));
    }
}
