package com.zioinfo.kintex.visitor.feed;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.visitor.VisitorAuth;
import com.zioinfo.kintex.visitor.feed.FeedDtos.FeedItemDto;
import com.zioinfo.kintex.visitor.feed.FeedDtos.RecordViewRequest;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
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;

import java.util.List;

/**
 * F-C7 최근 본 행사·부스 피드 API — 인증(본인만).
 */
@RestController
@RequestMapping("/api/visitor/feed")
public class FeedController {

    private final FeedService service;

    public FeedController(FeedService service) {
        this.service = service;
    }

    /** POST /api/visitor/feed/view — 상세 진입 조회 이력 기록. */
    @PostMapping("/view")
    public ApiResponse<Void> record(@AuthenticationPrincipal KintexPrincipal principal,
                                    @Valid @RequestBody RecordViewRequest req) {
        service.record(VisitorAuth.requireUserId(principal), req);
        return ApiResponse.ok(null);
    }

    /** GET /api/visitor/feed/recent?limit= — 최근 본 피드. */
    @GetMapping("/recent")
    public ApiResponse<List<FeedItemDto>> recent(@AuthenticationPrincipal KintexPrincipal principal,
                                                 @RequestParam(defaultValue = "20") int limit) {
        return ApiResponse.ok(service.recent(VisitorAuth.requireUserId(principal), limit));
    }
}
