package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.BookingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * 예약 요청 API
 * Node.js: app.use("/api/booking", bookingRoutes)
 */
@RestController
@RequestMapping("/api/booking")
@RequiredArgsConstructor
@Slf4j
public class BookingController {
    private final BookingService bookingService;

    /** GET /list — 목록 조회 (alias) */
    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBookingListAlias(
            @RequestParam Map<String, Object> params) {
        return ResponseEntity.ok(ApiResponse.success(bookingService.getBookingList(params)));
    }

    /** GET / — 예약 목록 조회, 응답: { bookings, newCount } */
    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBookingList(
            @RequestParam Map<String, Object> params) {
        return ResponseEntity.ok(ApiResponse.success(bookingService.getBookingList(params)));
    }

    /** POST /{id}/accept — 예약 수락 */
    @PostMapping("/{id}/accept")
    public ResponseEntity<ApiResponse<Map<String, Object>>> acceptBooking(
            @PathVariable String id) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(bookingService.acceptBooking(params)));
    }

    /** POST /{id}/reject — 예약 거부 */
    @PostMapping("/{id}/reject")
    public ResponseEntity<ApiResponse<Map<String, Object>>> rejectBooking(
            @PathVariable String id,
            @RequestBody(required = false) Map<String, Object> body) {
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("id", id);
        if (body != null && body.containsKey("rejection_reason")) {
            params.put("rejection_reason", body.get("rejection_reason"));
        }
        return ResponseEntity.ok(ApiResponse.success(bookingService.rejectBooking(params)));
    }
}
