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.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典管理控制器。
 * <p>提供字典类型和字典项的新增、编辑、删除、查询 RESTful API。</p>
 */
@Tag(name = "字典管理", description = "字典类型和字典项的新增、编辑、删除、查询")
@RestController
@RequestMapping("/api/dict")
public class DictController {

    private final DictService dictService;

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

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

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

    /**
     * 编辑字典类型。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "编辑字典类型")
    @PutMapping("/type/edit")
    public ApiResult<DictType> editType(@RequestBody DictType dictType) {
        DictType updated = dictService.updateType(dictType);
        if (updated == null) {
            return ApiResult.error(404, "字典类型不存在");
        }
        return ApiResult.success("编辑成功", updated);
    }

    /**
     * 删除字典类型。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "删除字典类型")
    @DeleteMapping("/type/delete/{id}")
    public ApiResult<Void> deleteType(@PathVariable Long id) {
        if (!dictService.deleteTypeById(id)) {
            return ApiResult.error(404, "字典类型不存在");
        }
        return ApiResult.success("删除成功", null);
    }

    /**
     * 查询字典类型列表。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "查询字典类型列表")
    @GetMapping("/type/list")
    public ApiResult<List<DictType>> listTypes(
            @Parameter(description = "类型名称（模糊匹配）") @RequestParam(required = false) String typeName) {
        return ApiResult.success(dictService.listTypes(typeName));
    }

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

    /**
     * 新增字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "新增字典项")
    @PostMapping("/item/add")
    public ApiResult<DictItem> addItem(@RequestBody DictItem dictItem) {
        return ApiResult.success("新增成功", dictService.saveItem(dictItem));
    }

    /**
     * 编辑字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "编辑字典项")
    @PutMapping("/item/edit")
    public ApiResult<DictItem> editItem(@RequestBody DictItem dictItem) {
        DictItem updated = dictService.updateItem(dictItem);
        if (updated == null) {
            return ApiResult.error(404, "字典项不存在");
        }
        return ApiResult.success("编辑成功", updated);
    }

    /**
     * 删除字典项。
     *
     * @requirement(name="字典管理", id="SRS-F-01-004")
     */
    @Operation(summary = "删除字典项")
    @DeleteMapping("/item/delete/{id}")
    public ApiResult<Void> deleteItem(@PathVariable Long id) {
        if (!dictService.deleteItemById(id)) {
            return ApiResult.error(404, "字典项不存在");
        }
        return ApiResult.success("删除成功", null);
    }

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