package com.zioinfo.kintex.cms;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.cms.dto.MicrositeDto;
import com.zioinfo.kintex.cms.dto.MicrositeSaveRequest;
import com.zioinfo.kintex.common.ApiResponse;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * M17 참가업체 마이크로사이트 편집 API (SCR-36). 인증 필수.
 * GET/PUT /api/exhibitors/&#123;exhibitorId&#125;/microsite — 섹션 JSON·테마·SEO·상태 저장/조회.
 * 공개 읽기는 {@link PublicMicrositeController}(/api/public/microsites, 무인증).
 */
@RestController
@RequestMapping("/api/exhibitors/{exhibitorId}/microsite")
public class MicrositeController {

    private final MicrositeService service;
    private final EventAccessGuard guard;

    public MicrositeController(MicrositeService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<MicrositeDto> get(@AuthenticationPrincipal KintexPrincipal principal,
                                         @PathVariable String exhibitorId) {
        guard.require(principal);
        return ApiResponse.ok(service.get(exhibitorId));
    }

    @PutMapping
    public ApiResponse<MicrositeDto> save(@AuthenticationPrincipal KintexPrincipal principal,
                                          @PathVariable String exhibitorId,
                                          @Valid @RequestBody MicrositeSaveRequest req) {
        guard.require(principal);
        return ApiResponse.ok(service.save(exhibitorId, req));
    }
}
