package com.sino.mci.channel.controller;

import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.channel.dto.ChannelAccountInitRequest;
import com.sino.mci.channel.dto.CreateChannelAccountRequest;
import com.sino.mci.channel.dto.HeartbeatRequest;
import com.sino.mci.channel.dto.UpdateStatusRequest;
import com.sino.mci.channel.entity.ChannelAccount;
import com.sino.mci.channel.entity.SyncTask;
import com.sino.mci.channel.service.ChannelAccountService;
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/channel-account")
@RequiredArgsConstructor
public class ChannelAccountController {

    private final ChannelAccountService accountService;

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

    @PutMapping("/{id}")
    public ApiResponse<?> updateAccount(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("id") Long id,
            @Valid @RequestBody CreateChannelAccountRequest request) {
        return ApiResponse.ok(accountService.updateAccount(id, tenantCode, request));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> deleteAccount(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("id") Long id) {
        accountService.deleteAccount(id, tenantCode);
        return ApiResponse.ok(null);
    }

    @PatchMapping("/{id}/status")
    public ApiResponse<?> updateAccountStatus(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("id") Long id,
            @Valid @RequestBody UpdateStatusRequest request) {
        return ApiResponse.ok(accountService.updateAccountStatus(id, tenantCode, request));
    }

    @GetMapping("/{id}/status")
    public ApiResponse<ChannelAccount> getAccountStatus(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("id") Long id) {
        return ApiResponse.ok(accountService.getAccountStatus(id, tenantCode));
    }

    @PostMapping("/heartbeat")
    public ApiResponse<ChannelAccount> heartbeat(@Valid @RequestBody HeartbeatRequest request) {
        return ApiResponse.ok(accountService.heartbeat(request.getAccountId(), request));
    }

    @PostMapping("/init")
    public ApiResponse<?> initAccount(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody ChannelAccountInitRequest request) {
        return ApiResponse.ok(accountService.initAccount(tenantCode, request));
    }

    @GetMapping("/list")
    public ApiResponse<List<ChannelAccount>> listAccounts() {
        return ApiResponse.ok(accountService.listAccounts(AuthContext.currentTenantCode()));
    }

    @PostMapping("/{account_id}/sync-contacts")
    public ApiResponse<SyncTask> syncContacts(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("account_id") Long accountId) {
        return ApiResponse.ok(accountService.submitContactsSyncTask(accountId));
    }

    @PostMapping("/{account_id}/sync-conversations")
    public ApiResponse<SyncTask> syncConversations(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @PathVariable("account_id") Long accountId) {
        return ApiResponse.ok(accountService.submitConversationsSyncTask(accountId));
    }
}
