package com.zioinfo.mall.payment;

import com.zioinfo.mall.common.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/** 결제 API — /api/mall/payment. 인증 고객. 카드/계좌번호 응답 미노출(마스킹만). */
@RestController
@RequestMapping("/api/mall/payment")
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentService service;

    @PostMapping
    public ApiResponse<MallPayment> pay(@RequestBody PaymentService.PayRequest req, Authentication auth) {
        return ApiResponse.ok(service.pay(auth.getName(), isAdmin(auth), req));
    }

    @PostMapping("/{id}/vbank-confirm")
    public ApiResponse<MallPayment> confirmVbank(@PathVariable Long id, Authentication auth) {
        return ApiResponse.ok(service.confirmVbank(auth.getName(), isAdmin(auth), id));
    }

    @GetMapping("/order/{orderId}")
    public ApiResponse<MallPayment> byOrder(@PathVariable Long orderId, Authentication auth) {
        return ApiResponse.ok(service.getByOrder(orderId, auth.getName(), isAdmin(auth)));
    }

    private boolean isAdmin(Authentication auth) {
        return auth.getAuthorities().stream()
                .anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN") || a.getAuthority().equals("ROLE_MANAGER"));
    }
}
