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.CreatePlatformDictionaryTypeRequest;
import com.writeoff.module.system.dto.UpdatePlatformDictionaryItemRequest;
import com.writeoff.module.system.model.PlatformDictionaryItem;
import com.writeoff.module.system.model.PlatformDictionaryType;
import com.writeoff.module.system.service.PlatformDictionaryService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

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

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

    @GetMapping
    @RequirePermission(value = "platform.dictionary.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_LIST")
    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("/types")
    @RequirePermission(value = "platform.dictionary.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_TYPE_LIST")
    public ApiResponse<List<PlatformDictionaryType>> listTypes(
            @RequestParam(value = "enabledOnly", required = false) Boolean enabledOnly
    ) {
        return ApiResponse.success(platformDictionaryService.listTypes(enabledOnly));
    }

    @PostMapping("/types")
    @RequirePermission(value = "platform.dictionary.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_TYPE_CREATE")
    public ApiResponse<PlatformDictionaryType> createType(@RequestBody @Valid CreatePlatformDictionaryTypeRequest request) {
        return ApiResponse.success(platformDictionaryService.createType(request));
    }

    @PostMapping
    @RequirePermission(value = "platform.dictionary.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_CREATE")
    public ApiResponse<PlatformDictionaryItem> create(@RequestBody @Valid CreatePlatformDictionaryItemRequest request) {
        return ApiResponse.success(platformDictionaryService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "platform.dictionary.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_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 = "platform.dictionary.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        platformDictionaryService.enable(id);
        return ApiResponse.success("ok");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "platform.dictionary.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_DICTIONARY_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        platformDictionaryService.disable(id);
        return ApiResponse.success("ok");
    }

    /**
     * 供专家模块读取的共享字典选项。
     */
    @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);
    }
}
