package com.sino.mci.admin.controller;

import com.sino.mci.admin.dto.TestWebhookRequest;
import com.sino.mci.admin.dto.TestWebhookResponse;
import com.sino.mci.admin.dto.TenantWebhookResponse;
import com.sino.mci.admin.dto.UpdateTenantWebhookRequest;
import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.admin.service.TenantService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/account/tenant")
@RequiredArgsConstructor
public class TenantController {

    private final TenantService tenantService;

    @GetMapping("/webhook")
    public ApiResponse<TenantWebhookResponse> getWebhookConfig() {
        return ApiResponse.ok(tenantService.getWebhookConfig(AuthContext.currentTenantCode()));
    }

    @PostMapping("/webhook")
    public ApiResponse<TenantWebhookResponse> updateWebhookConfig(
            @Valid @RequestBody UpdateTenantWebhookRequest request) {
        return ApiResponse.ok(tenantService.updateWebhookConfig(AuthContext.currentTenantCode(), request));
    }

    @PostMapping("/webhook/test")
    public ApiResponse<TestWebhookResponse> testWebhook(@Valid @RequestBody TestWebhookRequest request) {
        return ApiResponse.ok(tenantService.testWebhook(AuthContext.currentTenantCode(), request));
    }
}
