package com.zioinfo.kintex.ticket;

import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.ticket.dto.TicketDtos.LookupResultDto;
import com.zioinfo.kintex.ticket.dto.TicketDtos.OrderRequest;
import com.zioinfo.kintex.ticket.dto.TicketDtos.OrderResultDto;
import com.zioinfo.kintex.ticket.dto.TicketDtos.TicketProductDto;
import jakarta.validation.Valid;
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;

/**
 * 공개 입장권 API (M10·M9) — 전 경로 비인증 공개({@code /api/public/**} permitAll).
 * <p>보안: 응답에 구매자 PII 원문 미노출(마스킹만) · 스택트레이스 미노출(GlobalExceptionHandler).
 */
@RestController
@RequestMapping("/api/public/tickets")
public class TicketController {

    private final TicketService service;

    public TicketController(TicketService service) {
        this.service = service;
    }

    /** GET /api/public/tickets/{eventId} — 판매중 상품 목록(잔여수량·판매기간 서버 권위). */
    @GetMapping("/{eventId}")
    public ApiResponse<List<TicketProductDto>> products(@PathVariable String eventId) {
        return ApiResponse.ok(service.listProducts(eventId));
    }

    /** POST /api/public/tickets/{eventId}/orders — 예매(재고차감→PG승인→발권). */
    @PostMapping("/{eventId}/orders")
    public ApiResponse<OrderResultDto> createOrder(@PathVariable String eventId,
                                                   @Valid @RequestBody OrderRequest req) {
        return ApiResponse.ok(service.createOrder(eventId, req));
    }

    /** GET /api/public/tickets/lookup?orderNo=&contact= — 주문번호+연락처 조회(PII 마스킹). */
    @GetMapping("/lookup")
    public ApiResponse<LookupResultDto> lookup(@RequestParam String orderNo,
                                               @RequestParam String contact) {
        return ApiResponse.ok(service.lookup(orderNo, contact));
    }
}
