package com.example.demo.controller;

import com.example.demo.model.Dictionary;
import com.example.demo.service.DictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

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

/**
 * 数据字典管理控制器。
 * 提供数据字典的增删改查等 RESTful API 接口。
 */
@RestController
@RequestMapping("/api/dict")
@Tag(name = "字典管理", description = "数据字典维护接口（IMS-IF-ADMIN）")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    /**
     * 新增字典项。
     *
     * @requirement(name="字典管理", id="SRS-IMS_F-004")
     *
     * @param dictionary 字典项信息
     * @return 创建后的字典项
     */
    @PostMapping
    @Operation(summary = "新增字典项", description = "创建新的数据字典项")
    public Dictionary createDictionary(@RequestBody Dictionary dictionary) {
        return dictionaryService.createDictionary(dictionary);
    }

    /**
     * 编辑字典项。
     *
     * @requirement(name="字典管理", id="SRS-IMS_F-004")
     *
     * @param id         字典ID
     * @param dictionary 更新信息
     * @return 更新后的字典项
     */
    @PutMapping("/{id}")
    @Operation(summary = "编辑字典项", description = "根据ID更新字典项信息")
    public Dictionary updateDictionary(@PathVariable Long id, @RequestBody Dictionary dictionary) {
        return dictionaryService.updateDictionary(id, dictionary);
    }

    /**
     * 删除字典项。
     *
     * @requirement(name="字典管理", id="SRS-IMS_F-004")
     *
     * @param id 字典ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典项", description = "根据ID删除字典项")
    public Map<String, Object> deleteDictionary(@PathVariable Long id) {
        dictionaryService.deleteDictionary(id);
        return Map.of("message", "字典项删除成功", "id", id);
    }

    /**
     * 查询字典列表。
     *
     * @requirement(name="字典管理", id="SRS-IMS_F-004")
     *
     * @param dictType 字典类型（可选，筛选）
     * @return 字典项列表
     */
    @GetMapping
    @Operation(summary = "查询字典列表", description = "支持按字典类型筛选")
    public List<Dictionary> listDictionaries(@RequestParam(required = false) String dictType) {
        return dictionaryService.listDictionaries(dictType);
    }
}
