package cn.nianxx.thhotel.workflows.reservation.control;

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.security.common.dto.AuthenticatedUserContext;
import cn.nianxx.thhotel.platform.security.service.FrontendAuthorizationService;
import cn.nianxx.thhotel.workflows.reservation.common.request.ReservationInvoiceManualGenerationRequest;
import cn.nianxx.thhotel.workflows.reservation.common.result.ReservationInvoiceGenerationResult;
import cn.nianxx.thhotel.workflows.reservation.service.ReservationInvoiceGenerationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;

/**
 * Reservation Invoice 生成接口。第一版只提供手工生成入口。
 */
@RestController
@RequestMapping("/api/reservation/invoices")
public class ReservationInvoiceGenerationController {

    private final FrontendAuthorizationService authorizationService;
    private final ReservationInvoiceGenerationService invoiceGenerationService;

    /**
     * 注入前端鉴权服务和 Invoice 生成服务。
     */
    public ReservationInvoiceGenerationController(
            FrontendAuthorizationService authorizationService,
            ReservationInvoiceGenerationService invoiceGenerationService) {
        this.authorizationService = authorizationService;
        this.invoiceGenerationService = invoiceGenerationService;
    }

    /**
     * 手工生成 Proforma Invoice。需要登录、发票生成权限和酒店访问权。
     */
    @PostMapping("/manual-generations")
    public ResponseEntity<ReservationInvoiceGenerationResult> generateManualInvoice(
            @RequestBody ReservationInvoiceManualGenerationRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.RESERVATION_INVOICE_GENERATE.name());
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(invoiceGenerationService.generateManualInvoice(request, actor));
    }
}
