package com.erp.service;

import com.erp.common.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 예약 요청 서비스
 * Node.js: bookingService.ts — booking_requests 테이블, company_code 필터 없음
 */
@Service
@Slf4j
public class BookingService extends BaseService {
    @Autowired private CommonService commonService;

    /**
     * GET /api/booking — 예약 목록 조회
     * 응답: { bookings: [...], newCount: N }
     * newCount = pending 상태이고 5분 이내 생성된 건수
     */
    public Map<String, Object> getBookingList(Map<String, Object> params) {
        commonService.applyPagination(params);
        Number cntNum = sqlSession.selectOne("booking.getBookingListCnt", params);
        int totalCount = cntNum != null ? cntNum.intValue() : 0;
        List<Map<String, Object>> list = sqlSession.selectList("booking.getBookingList", params);
        Number newCntNum = sqlSession.selectOne("booking.getBookingNewCount", params);
        int newCount = newCntNum != null ? newCntNum.intValue() : 0;

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("bookings", list);
        result.put("new_count", newCount);
        result.put("total_count", totalCount);
        return result;
    }

    /** POST /api/booking/:id/accept — 예약 수락 */
    @Transactional
    public Map<String, Object> acceptBooking(Map<String, Object> params) {
        sqlSession.update("booking.acceptBooking", params);
        return params;
    }

    /** POST /api/booking/:id/reject — 예약 거부 */
    @Transactional
    public Map<String, Object> rejectBooking(Map<String, Object> params) {
        sqlSession.update("booking.rejectBooking", params);
        return params;
    }
}
