package com.smyhub.store.mengyastorebackendjava.controller.admin;

import com.smyhub.store.mengyastorebackendjava.dto.request.AdminChatMessageRequest;
import com.smyhub.store.mengyastorebackendjava.dto.response.ApiResponse;
import com.smyhub.store.mengyastorebackendjava.dto.response.ChatMessageResponse;
import com.smyhub.store.mengyastorebackendjava.service.ChatService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/chat")
@RequiredArgsConstructor
public class AdminChatController {

    private final ChatService chatService;

    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, List<ChatMessageResponse>>>> listConversations() {
        return ResponseEntity.ok(ApiResponse.ok(chatService.listAllConversations()));
    }

    @GetMapping("/{account}")
    public ResponseEntity<ApiResponse<List<ChatMessageResponse>>> getConversation(
            @PathVariable String account) {
        return ResponseEntity.ok(ApiResponse.ok(chatService.getConversation(account)));
    }

    @PostMapping("/{account}")
    public ResponseEntity<ApiResponse<ChatMessageResponse>> replyToUser(
            @PathVariable String account,
            @Valid @RequestBody AdminChatMessageRequest req) {
        ChatMessageResponse msg = chatService.sendAdminMessage(account, req.getContent());
        return ResponseEntity.ok(ApiResponse.ok(msg));
    }

    @DeleteMapping("/{account}")
    public ResponseEntity<ApiResponse<Void>> clearConversation(@PathVariable String account) {
        chatService.clearConversation(account);
        return ResponseEntity.ok(ApiResponse.ok(null));
    }
}
