package cn.novalon.gym.manage.notify.handler;

import cn.novalon.gym.manage.common.dto.PageRequest;
import cn.novalon.gym.manage.notify.core.domain.SysUserMessage;
import cn.novalon.gym.manage.notify.core.service.ISysUserMessageService;
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.Flux;
import reactor.core.publisher.Mono;

@Component
public class SysUserMessageHandler {

    private final ISysUserMessageService messageService;

    public SysUserMessageHandler(ISysUserMessageService messageService) {
        this.messageService = messageService;
    }

    public Mono<ServerResponse> getMessagesByUser(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        Flux<SysUserMessage> messages = messageService.getMessagesByUser(userId);
        return ServerResponse.ok().body(messages, SysUserMessage.class);
    }

    public Mono<ServerResponse> getMessagesByUserPage(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        
        int page = Integer.parseInt(request.queryParam("page").orElse("0"));
        int size = Integer.parseInt(request.queryParam("size").orElse("10"));
        String sort = request.queryParam("sort").orElse("createTime");
        String order = request.queryParam("order").orElse("desc");
        
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        pageRequest.setSort(sort);
        pageRequest.setOrder(order);
        
        return messageService.getMessagesByUserPage(userId, pageRequest)
                .flatMap(pageResponse -> ServerResponse.ok().bodyValue(pageResponse));
    }

    public Mono<ServerResponse> getUnreadCount(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        return messageService.getUnreadCount(userId)
                .flatMap(count -> ServerResponse.ok().bodyValue(count));
    }

    public Mono<ServerResponse> getUnreadList(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        Flux<SysUserMessage> messages = messageService.getUnreadMessages(userId);
        return ServerResponse.ok().body(messages, SysUserMessage.class);
    }

    public Mono<ServerResponse> getUnreadMessagesPage(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        
        int page = Integer.parseInt(request.queryParam("page").orElse("0"));
        int size = Integer.parseInt(request.queryParam("size").orElse("10"));
        String sort = request.queryParam("sort").orElse("createTime");
        String order = request.queryParam("order").orElse("desc");
        
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        pageRequest.setSort(sort);
        pageRequest.setOrder(order);
        
        return messageService.getUnreadMessagesPage(userId, pageRequest)
                .flatMap(pageResponse -> ServerResponse.ok().bodyValue(pageResponse));
    }

    public Mono<ServerResponse> createMessage(ServerRequest request) {
        return request.bodyToMono(SysUserMessage.class)
                .flatMap(messageService::createMessage)
                .flatMap(message -> ServerResponse.ok().bodyValue(message));
    }

    public Mono<ServerResponse> markAsRead(ServerRequest request) {
        Long id = Long.parseLong(request.pathVariable("id"));
        return messageService.markAsRead(id)
                .flatMap(message -> ServerResponse.ok().bodyValue(message))
                .switchIfEmpty(ServerResponse.notFound().build());
    }

    public Mono<ServerResponse> markAllAsRead(ServerRequest request) {
        Long userId = Long.parseLong(request.pathVariable("userId"));
        return messageService.markAllAsRead(userId)
                .flatMap(count -> ServerResponse.ok().bodyValue(count));
    }

    public Mono<ServerResponse> deleteMessage(ServerRequest request) {
        Long id = Long.parseLong(request.pathVariable("id"));
        return messageService.deleteMessage(id)
                .then(ServerResponse.ok().build())
                .switchIfEmpty(ServerResponse.notFound().build());
    }
}