package com.zioinfo.kintex.module.m4;

import com.zioinfo.kintex.auth.EventAccessGuard;
import com.zioinfo.kintex.auth.EventRole;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.module.m4.dto.UtilityOrderDto;
import com.zioinfo.kintex.module.m4.dto.UtilityQuote;
import com.zioinfo.kintex.module.m4.dto.UtilityQuoteRequest;
import com.zioinfo.kintex.module.m4.dto.WiringRequest;
import com.zioinfo.kintex.module.m4.dto.WiringResult;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * M4 유틸리티 설계 API (SCR-07/08). 참가업체(신청)·장치업체(검토). 행사 RBAC 가드.
 */
@RestController
@RequestMapping("/api/events/{eventId}/booths/{boothId}/utility")
public class UtilityController {

    private final UtilityService service;
    private final EventAccessGuard guard;

    public UtilityController(UtilityService service, EventAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** POST /quote — 자동 견적(요율 룰셋 기반, 완성 구현). */
    @PostMapping("/quote")
    public ApiResponse<UtilityQuote> quote(@AuthenticationPrincipal KintexPrincipal principal,
                                           @PathVariable String eventId,
                                           @PathVariable String boothId,
                                           @Valid @RequestBody UtilityQuoteRequest request) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.quote(request));
    }

    /** POST /wiring — 배선 산출(트렌치 최단 경로). */
    @PostMapping("/wiring")
    public ApiResponse<WiringResult> wiring(@AuthenticationPrincipal KintexPrincipal principal,
                                            @PathVariable String eventId,
                                            @PathVariable String boothId,
                                            @RequestParam String hallId,
                                            @Valid @RequestBody WiringRequest request) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.computeWiring(eventId, hallId, boothId, request));
    }

    /** POST /order — 신청 제출 + 위치표시도 생성 트리거(참가업체). */
    @PostMapping("/order")
    public ApiResponse<UtilityOrderDto> order(@AuthenticationPrincipal KintexPrincipal principal,
                                              @PathVariable String eventId,
                                              @PathVariable String boothId,
                                              @Valid @RequestBody UtilityQuoteRequest request) {
        guard.requireRole(principal, eventId, EventRole.EXHIBITOR, EventRole.CONTRACTOR);
        return ApiResponse.ok(service.submitOrder(eventId, boothId, request));
    }

    /** GET — 신청 조회. */
    @GetMapping
    public ApiResponse<UtilityOrderDto> get(@AuthenticationPrincipal KintexPrincipal principal,
                                            @PathVariable String eventId,
                                            @PathVariable String boothId) {
        guard.requireEventAccess(principal, eventId);
        return ApiResponse.ok(service.getOrder(eventId, boothId));
    }
}
