package com.zioinfo.kintex.publicsite;

import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.publicsite.dto.InquiryReceiptDto;
import com.zioinfo.kintex.publicsite.dto.InquiryRequest;
import com.zioinfo.kintex.publicsite.dto.PublicEventDto;
import com.zioinfo.kintex.publicsite.dto.PublicFloorplanDto;
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;

import java.util.List;

/**
 * 공개 홍보 사이트(M12) API — 전 경로 비인증 공개(SecurityConfig 에서 {@code /api/public/**} permitAll).
 * <p>공개 카탈로그(행사·플로어플랜)는 read-only, 문의는 접수만. 내부 식별자·PII·원가 미노출.
 * <p>관람객 사전등록(POST {@code /api/public/events/{id}/register})은 visitor 트랙 소관 — 여기서 정의하지 않는다.
 */
@RestController
@RequestMapping("/api/public")
public class PublicSiteController {

    private static final int MAX_LIMIT = 2000;

    private final PublicSiteService service;

    public PublicSiteController(PublicSiteService service) {
        this.service = service;
    }

    /** GET /api/public/events?year=&q= — 공개 카탈로그(시작일 내림차순). */
    @GetMapping("/events")
    public ApiResponse<List<PublicEventDto>> events(@RequestParam(required = false) String year,
                                                    @RequestParam(required = false) String q,
                                                    @RequestParam(required = false, defaultValue = "0") int page,
                                                    @RequestParam(required = false, defaultValue = "2000") int size) {
        int limit = Math.min(Math.max(size, 1), MAX_LIMIT);
        int offset = Math.max(page, 0) * limit;
        return ApiResponse.ok(service.listEvents(year, q, limit, offset));
    }

    /** GET /api/public/events/{eventId} — 공개 행사 상세(공개 필드만). */
    @GetMapping("/events/{eventId}")
    public ApiResponse<PublicEventDto> event(@PathVariable String eventId) {
        return ApiResponse.ok(service.getEvent(eventId));
    }

    /** GET /api/public/events/{eventId}/floorplan — 공개 플로어플랜 요약(부스번호·업체명·상태·중심점). */
    @GetMapping("/events/{eventId}/floorplan")
    public ApiResponse<PublicFloorplanDto> floorplan(@PathVariable String eventId) {
        return ApiResponse.ok(service.getFloorplan(eventId));
    }

    /** POST /api/public/inquiries — 참가/부스 신청 문의 접수(접수번호 반환). */
    @PostMapping("/inquiries")
    public ApiResponse<InquiryReceiptDto> inquiry(@RequestBody InquiryRequest req) {
        return ApiResponse.ok(service.submitInquiry(req));
    }
}
