package cn.novalon.gym.manage.sys.handler.dictionary;

import cn.novalon.gym.manage.sys.core.domain.Dictionary;
import cn.novalon.gym.manage.sys.core.service.IDictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * 字典处理器
 * 
 * @author 张翔
 * @date 2026-03-14
 */
@Component
@Tag(name = "字典管理", description = "字典数据相关操作")
public class DictionaryHandler {

    private final IDictionaryService dictionaryService;

    public DictionaryHandler(IDictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @Operation(summary = "获取所有字典", description = "获取系统中所有字典列表")
    public Mono<ServerResponse> getAllDictionaries(ServerRequest request) {
        return ServerResponse.ok()
                .body(dictionaryService.findAll(), Dictionary.class);
    }

    @Operation(summary = "根据ID获取字典", description = "根据字典ID获取字典详细信息")
    public Mono<ServerResponse> getDictionaryById(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return dictionaryService.findById(id)
                .flatMap(dict -> ServerResponse.ok().bodyValue(dict))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

    @Operation(summary = "根据类型获取字典", description = "根据字典类型获取字典列表")
    public Mono<ServerResponse> getDictionariesByType(ServerRequest request) {
        String type = request.pathVariable("type");
        return ServerResponse.ok()
                .body(dictionaryService.findByType(type), Dictionary.class);
    }

    @Operation(summary = "检查字典存在性", description = "检查指定类型和代码的字典是否存在")
    public Mono<ServerResponse> checkTypeAndCodeExists(ServerRequest request) {
        String type = request.queryParam("type").orElse(null);
        String code = request.queryParam("code").orElse(null);
        return dictionaryService.checkTypeAndCodeExists(type, code)
                .flatMap(exists -> ServerResponse.ok().bodyValue(exists));
    }

    @Operation(summary = "创建字典", description = "创建新字典")
    public Mono<ServerResponse> createDictionary(ServerRequest request) {
        return request.bodyToMono(Dictionary.class)
                .flatMap(dictionaryService::save)
                .flatMap(dict -> ServerResponse.status(HttpStatus.CREATED).bodyValue(dict));
    }

    @Operation(summary = "更新字典", description = "更新字典信息")
    public Mono<ServerResponse> updateDictionary(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return request.bodyToMono(Dictionary.class)
                .flatMap(dictionary -> dictionaryService.update(id, dictionary))
                .flatMap(dict -> ServerResponse.ok().bodyValue(dict))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

    @Operation(summary = "删除字典", description = "删除指定字典")
    public Mono<ServerResponse> deleteDictionary(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return dictionaryService.deleteById(id)
                .then(ServerResponse.noContent().build());
    }
}
