package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.model.DictItem;
import com.example.demo.model.DictType;
import com.example.demo.service.DictService;
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;

/**
 * 字典管理控制器。
 * <p>提供字典类型和字典项的新增、编辑、删除、查询接口。</p>
 */
@RestController
@RequestMapping("/api/dict")
@Tag(name = "字典管理", description = "字典管理模块接口（需求标识：SRS-F-01-004）")
public class DictController {

    private final DictService dictService;

    public DictController(DictService dictService) {
        this.dictService = dictService;
    }

    // ========== 字典类型 ==========

    /**
     * 新增字典类型。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @PostMapping("/type/add")
    @Operation(summary = "新增字典类型", description = "创建新的字典类型")
    public ApiResult<DictType> addType(@RequestBody DictType type) {
        return ApiResult.success(dictService.addDictType(type));
    }

    /**
     * 编辑字典类型。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @PutMapping("/type/edit")
    @Operation(summary = "编辑字典类型", description = "修改已有字典类型")
    public ApiResult<DictType> editType(@RequestBody DictType type) {
        return ApiResult.success(dictService.editDictType(type));
    }

    /**
     * 删除字典类型。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @DeleteMapping("/type/delete/{id}")
    @Operation(summary = "删除字典类型", description = "根据ID删除字典类型及其字典项")
    public ApiResult<Void> deleteType(@PathVariable Long id) {
        dictService.deleteDictType(id);
        return ApiResult.success();
    }

    /**
     * 查询字典类型列表。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @GetMapping("/type/list")
    @Operation(summary = "查询字典类型列表", description = "返回所有字典类型")
    public ApiResult<List<DictType>> listTypes() {
        return ApiResult.success(dictService.listDictTypes());
    }

    // ========== 字典项 ==========

    /**
     * 新增字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @PostMapping("/item/add")
    @Operation(summary = "新增字典项", description = "为指定字典类型添加字典项")
    public ApiResult<DictItem> addItem(@RequestBody DictItem item) {
        return ApiResult.success(dictService.addDictItem(item));
    }

    /**
     * 编辑字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @PutMapping("/item/edit")
    @Operation(summary = "编辑字典项", description = "修改字典项信息")
    public ApiResult<DictItem> editItem(@RequestBody DictItem item) {
        return ApiResult.success(dictService.editDictItem(item));
    }

    /**
     * 删除字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @DeleteMapping("/item/delete/{id}")
    @Operation(summary = "删除字典项", description = "根据ID删除字典项")
    public ApiResult<Void> deleteItem(@PathVariable Long id) {
        dictService.deleteDictItem(id);
        return ApiResult.success();
    }

    /**
     * 查询字典项列表。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @GetMapping("/item/list")
    @Operation(summary = "查询字典项列表", description = "根据字典类型ID查询其下所有字典项")
    public ApiResult<List<DictItem>> listItems(@RequestParam Long typeId) {
        return ApiResult.success(dictService.listDictItems(typeId));
    }
}
