package com.zioinfo.kintex.webhook;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import com.zioinfo.kintex.webhook.dto.WebhookDtos.*;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * F099 웹훅 게이트웨이 관리 API — 시스템관리자(홀매니저/ADMIN)만 접근({@link SystemAccessGuard#requireAdmin}).
 * 아웃바운드 구독 CRUD + 인바운드 엔드포인트 등록 + 발송 큐(스텁)/수신 로그 조회.
 * <b>후속 웨이브: /screens/admin 웹훅 구독 관리 화면(계약 문서 참조)</b>.
 */
@RestController
@RequestMapping("/api/admin/webhooks")
public class WebhookAdminController {

    private final WebhookService service;
    private final SystemAccessGuard guard;

    public WebhookAdminController(WebhookService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    // ── 아웃바운드 구독 ──
    @GetMapping("/subscriptions")
    public ApiResponse<List<SubscriptionDto>> subscriptions(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.listSubscriptions());
    }

    @Audited(action = "WEBHOOK_SUB_CREATE", targetType = "webhook_subscription")
    @PostMapping("/subscriptions")
    public ApiResponse<SubscriptionDto> createSubscription(@AuthenticationPrincipal KintexPrincipal principal,
                                                           @RequestBody SubscriptionCreateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.createSubscription(req, principal.userId()));
    }

    @Audited(action = "WEBHOOK_SUB_UPDATE", targetType = "webhook_subscription")
    @PutMapping("/subscriptions/{id}")
    public ApiResponse<SubscriptionDto> updateSubscription(@AuthenticationPrincipal KintexPrincipal principal,
                                                           @PathVariable String id,
                                                           @RequestBody SubscriptionUpdateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.updateSubscription(id, req));
    }

    @Audited(action = "WEBHOOK_SUB_DELETE", targetType = "webhook_subscription")
    @DeleteMapping("/subscriptions/{id}")
    public ApiResponse<Void> deleteSubscription(@AuthenticationPrincipal KintexPrincipal principal,
                                                @PathVariable String id) {
        guard.requireAdmin(principal);
        service.deleteSubscription(id);
        return ApiResponse.ok(null);
    }

    // ── 아웃바운드 발송 큐(스텁 조회) ──
    @GetMapping("/deliveries")
    public ApiResponse<List<DeliveryDto>> deliveries(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.listDeliveries());
    }

    // ── 인바운드 엔드포인트 ──
    @GetMapping("/inbound")
    public ApiResponse<List<InboundEndpointDto>> inbound(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.listInboundEndpoints());
    }

    @Audited(action = "WEBHOOK_INBOUND_CREATE", targetType = "webhook_inbound_endpoint")
    @PostMapping("/inbound")
    public ApiResponse<InboundEndpointDto> createInbound(@AuthenticationPrincipal KintexPrincipal principal,
                                                         @RequestBody InboundEndpointCreateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.createInboundEndpoint(req, principal.userId()));
    }
}
