package com.sino.mci.channel.controller;

import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.channel.dto.BindConversationAccountRequest;
import com.sino.mci.channel.dto.BindConversationTagsRequest;
import com.sino.mci.channel.dto.ConversationParticipantResponse;
import com.sino.mci.channel.dto.CreateConversationRequest;
import com.sino.mci.channel.dto.TagResponse;
import com.sino.mci.channel.dto.UpdateConversationRequest;
import com.sino.mci.channel.entity.Conversation;
import com.sino.mci.channel.service.ConversationService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/conversation")
@RequiredArgsConstructor
public class ConversationController {

    private final ConversationService conversationService;

    @PostMapping
    public ApiResponse<?> createConversation(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody CreateConversationRequest request) {
        return ApiResponse.ok(conversationService.createConversation(tenantCode, request));
    }

    @GetMapping("/list")
    public ApiResponse<List<Conversation>> listConversations() {
        return ApiResponse.ok(conversationService.listConversations(AuthContext.currentTenantCode()));
    }

    @GetMapping("/{conversation_id}")
    public ApiResponse<?> getConversation(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId) {
        return ApiResponse.ok(conversationService.getConversation(tenantCode, conversationId));
    }

    @PutMapping("/{conversation_id}")
    public ApiResponse<?> updateConversation(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId,
            @Valid @RequestBody UpdateConversationRequest request) {
        return ApiResponse.ok(conversationService.updateConversation(tenantCode, conversationId, request));
    }

    @DeleteMapping("/{conversation_id}")
    public ApiResponse<?> deleteConversation(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId) {
        conversationService.deleteConversation(tenantCode, conversationId);
        return ApiResponse.ok();
    }

    @PostMapping("/{conversation_id}/bind-account")
    public ApiResponse<?> bindAccount(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId,
            @Valid @RequestBody BindConversationAccountRequest request) {
        conversationService.bindAccount(tenantCode, conversationId, request);
        return ApiResponse.ok();
    }

    @PostMapping("/{conversation_id}/bind-tags")
    public ApiResponse<?> bindTags(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId,
            @Valid @RequestBody BindConversationTagsRequest request) {
        conversationService.bindTags(tenantCode, conversationId, request);
        return ApiResponse.ok();
    }

    @GetMapping("/{conversation_id}/participants")
    public ApiResponse<List<ConversationParticipantResponse>> listParticipants(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId) {
        return ApiResponse.ok(conversationService.listParticipants(tenantCode, conversationId));
    }

    @GetMapping("/{conversation_id}/tags")
    public ApiResponse<List<TagResponse>> listTags(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("conversation_id") Long conversationId) {
        return ApiResponse.ok(conversationService.listTags(tenantCode, conversationId));
    }
}
