package com.zioinfo.kintex.marketing;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.EventRole;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.marketing.dto.MarketingDtos.*;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * M12 EDM·캠페인 · 스폰서십 API (SCR-33·SCR-34). 모두 인증 필수({@link EventAccessGuard}).
 * 캠페인 생성은 주최자/홀매니저 역할 게이트. 실 발송은 미구현(status 전이만).
 */
@RestController
@RequestMapping("/api/events/{eventId}")
public class MarketingController {

    private final MarketingService service;
    private final EventAccessGuard guard;

    public MarketingController(MarketingService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** 캠페인 목록(선택 status 필터). */
    @GetMapping("/campaigns")
    public ApiResponse<List<CampaignDto>> campaigns(@AuthenticationPrincipal KintexPrincipal principal,
                                                    @PathVariable String eventId,
                                                    @RequestParam(required = false) String status) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.campaigns(eventId, status));
    }

    /** 캠페인 생성(발송 미구현 — status 전이만). */
    @Audited(action = "CAMPAIGN_CREATE", targetType = "edm_campaign")
    @PostMapping("/campaigns")
    public ApiResponse<CampaignDto> createCampaign(@AuthenticationPrincipal KintexPrincipal principal,
                                                   @PathVariable String eventId,
                                                   @RequestBody CampaignCreateRequest req) {
        guard.requireRole(principal, eventId, EventRole.ORGANIZER, EventRole.HALL_MANAGER);
        return ApiResponse.ok(service.createCampaign(eventId, req));
    }

    /** 캠페인 발송(수신동의자 한정) — 매니저 이상. 정보통신망법 준수·회당 상한·배치 분할은 서비스가 강제. */
    @Audited(action = "CAMPAIGN_SEND", targetType = "edm_campaign")
    @PostMapping("/campaigns/{id}/send")
    public ApiResponse<CampaignSendResult> sendCampaign(@AuthenticationPrincipal KintexPrincipal principal,
                                                        @PathVariable String eventId,
                                                        @PathVariable String id) {
        guard.requireRole(principal, eventId, EventRole.ORGANIZER, EventRole.HALL_MANAGER);
        return ApiResponse.ok(service.sendCampaign(eventId, id));
    }

    /** 스폰서십 패키지·판매 현황. */
    @GetMapping("/sponsorship")
    public ApiResponse<SponsorshipView> sponsorship(@AuthenticationPrincipal KintexPrincipal principal,
                                                    @PathVariable String eventId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.sponsorship(eventId));
    }
}
