package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.module.system.dto.CreatePlatformDictionaryItemRequest;
import com.writeoff.module.system.dto.UpdatePlatformDictionaryItemRequest;
import com.writeoff.module.system.model.PlatformDictionaryItem;
import com.writeoff.module.system.service.PlatformDictionaryService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/dictionaries")
public class DictionaryController {
    private final PlatformDictionaryService platformDictionaryService;

    public DictionaryController(PlatformDictionaryService platformDictionaryService) {
        this.platformDictionaryService = platformDictionaryService;
    }

    /**
     * 租户端通用字典查询（只读）。
     */
    @GetMapping
    public ApiResponse<List<PlatformDictionaryItem>> list(
            @RequestParam(value = "dictType", required = false) String dictType,
            @RequestParam(value = "enabledOnly", required = false) Boolean enabledOnly
    ) {
        return ApiResponse.success(platformDictionaryService.list(dictType, enabledOnly));
    }

    /**
     * 租户端与平台端共享读取专家字典选项。
     */
    @GetMapping("/expert-options")
    public ApiResponse<Map<String, List<PlatformDictionaryItem>>> expertOptions() {
        Map<String, List<PlatformDictionaryItem>> data = new LinkedHashMap<String, List<PlatformDictionaryItem>>();
        data.put("titles", platformDictionaryService.list("EXPERT_TITLE", true));
        data.put("hospitals", platformDictionaryService.list("EXPERT_HOSPITAL", true));
        return ApiResponse.success(data);
    }

    /**
     * 租户端新增字典项。
     */
    @PostMapping
    @RequirePermission(value = "dictionary.manage", dataScope = DataScopeType.TENANT, auditAction = "DICTIONARY_CREATE")
    public ApiResponse<PlatformDictionaryItem> create(@RequestBody @Valid CreatePlatformDictionaryItemRequest request) {
        return ApiResponse.success(platformDictionaryService.create(request));
    }

    /**
     * 租户端更新字典项。
     */
    @PutMapping("/{id}")
    @RequirePermission(value = "dictionary.manage", dataScope = DataScopeType.TENANT, auditAction = "DICTIONARY_UPDATE")
    public ApiResponse<PlatformDictionaryItem> update(@PathVariable("id") Long id,
                                                       @RequestBody @Valid UpdatePlatformDictionaryItemRequest request) {
        return ApiResponse.success(platformDictionaryService.update(id, request));
    }

    /**
     * 租户端启用字典项。
     */
    @PostMapping("/{id}/enable")
    @RequirePermission(value = "dictionary.manage", dataScope = DataScopeType.TENANT, auditAction = "DICTIONARY_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        platformDictionaryService.enable(id);
        return ApiResponse.success("OK");
    }

    /**
     * 租户端停用字典项。
     */
    @PostMapping("/{id}/disable")
    @RequirePermission(value = "dictionary.manage", dataScope = DataScopeType.TENANT, auditAction = "DICTIONARY_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        platformDictionaryService.disable(id);
        return ApiResponse.success("OK");
    }
}
