package com.sourcefellows.patterns.remotefacade;

public class BookingFacade {

    private final HotelService hotelService;
    private final UserService userService;
    private final PaymentService paymentService;

    public BookingFacade(HotelService hotelService,
                         UserService userService,
                         PaymentService paymentService) {

        this.hotelService = hotelService;
        this.userService = userService;
        this.paymentService = paymentService;
    }

    // Methode zur Buchung eines Hotels
    public BookingResponse bookHotel(String userId,
                                     String hotelId,
                                     String paymentDetails) {
        // 1. Benutzer überprüfen
        var user = userService.getUserById(userId);
        if (user == null) {
            throw new IllegalArgumentException("Benutzer nicht gefunden!");
        }

        // 2. Hotelverfügbarkeit prüfen
        var hotel = hotelService.getHotelById(hotelId);
        if (!hotel.isAvailable()) {
            throw new IllegalStateException("Hotel ist nicht verfügbar!");
        }

        // 3. Zahlung durchführen
        var paymentSuccess =
                paymentService.processPayment(user, hotel, paymentDetails);
        if (!paymentSuccess) {
            throw new RuntimeException("Zahlung fehlgeschlagen!");
        }

        // 4. Buchung bestätigen
        var response = new BookingResponse();
        response.setUser(user);
        response.setHotel(hotel);
        response.setStatus("Buchung erfolgreich!");

        return response;
    }
}
