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

import cn.novalon.gym.manage.sys.core.domain.SysDictType;
import cn.novalon.gym.manage.sys.core.domain.SysDictData;
import cn.novalon.gym.manage.sys.core.service.ISysDictTypeService;
import cn.novalon.gym.manage.sys.core.service.ISysDictDataService;
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 SysDictHandler {

    private final ISysDictTypeService dictTypeService;
    private final ISysDictDataService dictDataService;

    public SysDictHandler(ISysDictTypeService dictTypeService, ISysDictDataService dictDataService) {
        this.dictTypeService = dictTypeService;
        this.dictDataService = dictDataService;
    }

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

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

    @Operation(summary = "根据类型获取字典类型", description = "根据字典类型代码获取详细信息")
    public Mono<ServerResponse> getDictTypeByType(ServerRequest request) {
        String dictType = request.pathVariable("dictType");
        return dictTypeService.findByDictType(dictType)
                .flatMap(type -> ServerResponse.ok().bodyValue(type))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

    @Operation(summary = "创建字典类型", description = "创建新的字典类型")
    public Mono<ServerResponse> createDictType(ServerRequest request) {
        return request.bodyToMono(SysDictType.class)
                .flatMap(dictTypeService::save)
                .flatMap(dt -> ServerResponse.status(HttpStatus.CREATED).bodyValue(dt));
    }

    @Operation(summary = "更新字典类型", description = "更新字典类型信息")
    public Mono<ServerResponse> updateDictType(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return request.bodyToMono(SysDictType.class)
                .flatMap(dictType -> dictTypeService.findById(id)
                        .flatMap(existing -> {
                            existing.setDictName(dictType.getDictName());
                            existing.setStatus(dictType.getStatus());
                            existing.setRemark(dictType.getRemark());
                            return dictTypeService.save(existing);
                        }))
                .flatMap(updated -> ServerResponse.ok().bodyValue(updated))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

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

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

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

    @Operation(summary = "根据类型获取字典数据", description = "根据字典类型获取字典数据列表")
    public Mono<ServerResponse> getDictDataByType(ServerRequest request) {
        String dictType = request.pathVariable("dictType");
        return ServerResponse.ok()
                .body(dictDataService.findByDictType(dictType), SysDictData.class);
    }

    @Operation(summary = "创建字典数据", description = "创建新的字典数据")
    public Mono<ServerResponse> createDictData(ServerRequest request) {
        return request.bodyToMono(SysDictData.class)
                .flatMap(dictDataService::save)
                .flatMap(dd -> ServerResponse.status(HttpStatus.CREATED).bodyValue(dd));
    }

    @Operation(summary = "更新字典数据", description = "更新字典数据信息")
    public Mono<ServerResponse> updateDictData(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return request.bodyToMono(SysDictData.class)
                .flatMap(dictData -> dictDataService.findById(id)
                        .flatMap(existing -> {
                            existing.setDictLabel(dictData.getDictLabel());
                            existing.setDictValue(dictData.getDictValue());
                            existing.setDictSort(dictData.getDictSort());
                            existing.setCssClass(dictData.getCssClass());
                            existing.setListClass(dictData.getListClass());
                            existing.setIsDefault(dictData.getIsDefault());
                            existing.setStatus(dictData.getStatus());
                            return dictDataService.save(existing);
                        }))
                .flatMap(updated -> ServerResponse.ok().bodyValue(updated))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

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