package com.zioinfo.kintex.publicsite.subscription;

import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.publicsite.subscription.dto.SubscriptionDtos.SubscribeRequest;
import com.zioinfo.kintex.publicsite.subscription.dto.SubscriptionDtos.SubscribeResult;
import com.zioinfo.kintex.publicsite.subscription.dto.SubscriptionDtos.UnsubscribeResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 관심 행사 구독(F-C1) 공개 API — 전 경로 비인증({@code /api/public/**} permitAll).
 * <p>구독 신청/해지만 제공. 구독 목록·원문 이메일 조회는 공개하지 않는다(§0-3).
 * 티켓 오픈 시 알림 발송은 {@link TicketOpenNotifier}(스케줄러)가 담당한다.
 */
@RestController
@RequestMapping("/api/public")
public class EventSubscriptionController {

    private final EventSubscriptionService service;

    public EventSubscriptionController(EventSubscriptionService service) {
        this.service = service;
    }

    /** POST /api/public/events/{eventId}/subscribe — 관심 행사 구독(이메일·개인정보 동의). */
    @PostMapping("/events/{eventId}/subscribe")
    public ApiResponse<SubscribeResult> subscribe(@PathVariable String eventId,
                                                  @RequestBody SubscribeRequest req) {
        return ApiResponse.ok(service.subscribe(eventId, req));
    }

    /** POST /api/public/subscriptions/unsubscribe?token= — 원클릭 수신거부(멱등). */
    @PostMapping("/subscriptions/unsubscribe")
    public ApiResponse<UnsubscribeResult> unsubscribe(@RequestParam String token) {
        return ApiResponse.ok(service.unsubscribe(token));
    }

    /** GET /api/public/subscriptions/unsubscribe?token= — 메일 링크(GET) 호환. */
    @GetMapping("/subscriptions/unsubscribe")
    public ApiResponse<UnsubscribeResult> unsubscribeGet(@RequestParam String token) {
        return ApiResponse.ok(service.unsubscribe(token));
    }
}
